Skip to content

Commit

Permalink
Add support for named volumes
Browse files Browse the repository at this point in the history
Closes #13
  • Loading branch information
felipecrs committed Oct 16, 2024
1 parent 317ba14 commit ddd9487
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 93 deletions.
2 changes: 1 addition & 1 deletion dond
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ function set_parent_container_mounts() {
local docker_output
docker_output=$(
"${docker_path}" inspect \
--format '{{range .Mounts}}{{if eq .Type "bind"}}{{printf "%s:%s\n" .Source .Destination}}{{end}}{{end}}' \
--format '{{range .Mounts}}{{if or (eq .Type "bind") (eq .Type "volume")}}{{printf "%s:%s\n" .Source .Destination}}{{end}}{{end}}' \
"${container_id}"
)

Expand Down
194 changes: 102 additions & 92 deletions scripts/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -38,97 +38,107 @@ for docker_version in "${docker_versions[@]}"; do

image_id="$(docker build --target test --build-arg "DOCKER_VERSION=${docker_version}" --quiet .)"

echo "Do not change global options or after the image"
"${docker_args[@]}" --env DOND_SHIM_PRINT_COMMAND=true --volume "${fixtures_dir}:/wd" "${image_id}" \
docker --host test run --volume /wd:/wd alpine --volume /wd:/wd |
grep --quiet "^docker.orig --host test run --volume ${fixtures_dir}:/wd alpine --volume /wd:/wd$"

echo "Same as above, but retaining read only mode"
"${docker_args[@]}" --env DOND_SHIM_PRINT_COMMAND=true --volume "${fixtures_dir}:/wd" "${image_id}" \
docker --host test run --volume /wd:/wd:ro alpine --volume /wd:/wd |
grep --quiet "^docker.orig --host test run --volume ${fixtures_dir}:/wd:ro alpine --volume /wd:/wd$"

echo "Same as above but with --mount"
"${docker_args[@]}" --env DOND_SHIM_PRINT_COMMAND=true --volume "${fixtures_dir}:/wd" "${image_id}" \
docker --host test run --volume /wd:/wd:ro --mount=type=bind,source=/wd,readonly,destination=/wd2 alpine --volume /wd:/wd |
grep --quiet "^docker.orig --host test run --volume ${fixtures_dir}:/wd:ro --mount=type=bind,source=${fixtures_dir},readonly,destination=/wd2 alpine --volume /wd:/wd$"

echo "Same as above (without --mount), but retaining read only mode on auto added volume"
"${docker_args[@]}" --env DOND_SHIM_PRINT_COMMAND=true --env DOND_SHIM_MOCK_CONTAINER_ROOT_ON_HOST=/container-root --volume "${fixtures_dir}:/wd" --volume "${fixtures_dir}/testfile:/test/testfile" "${image_id}" \
docker --host test run --volume /wd:/wd:ro --volume /test:/test:ro alpine --volume /wd:/wd |
grep --quiet "^docker.orig --host test run --volume ${fixtures_dir}:/wd:ro --volume /container-root/test:/test:ro --volume ${fixtures_dir}/testfile:/test/testfile:ro alpine --volume /wd:/wd$"

echo "Same as above but should not auto add mounts which are not bind mounts"
"${docker_args[@]}" --env DOND_SHIM_PRINT_COMMAND=true --env DOND_SHIM_MOCK_CONTAINER_ROOT_ON_HOST=/container-root --volume "${fixtures_dir}:/wd" --volume "${fixtures_dir}/testfile:/test/testfile" --mount type=tmpfs,target=/test/tmpfsdir "${image_id}" \
docker --host test run --volume /wd:/wd:ro --volume /test:/test:ro alpine --volume /wd:/wd |
grep --quiet "^docker.orig --host test run --volume ${fixtures_dir}:/wd:ro --volume /container-root/test:/test:ro --volume ${fixtures_dir}/testfile:/test/testfile:ro alpine --volume /wd:/wd$"

echo "Same as above (with --mount src and target, dst), but retaining read only mode on auto added volume"
"${docker_args[@]}" --env DOND_SHIM_PRINT_COMMAND=true --env DOND_SHIM_MOCK_CONTAINER_ROOT_ON_HOST=/container-root --volume "${fixtures_dir}:/wd" --volume "${fixtures_dir}/testfile:/test/testfile" "${image_id}" \
docker --host test run --mount type=bind,src=/wd,target=/wd,readonly --mount type=bind,source=/test,dst=/test,readonly alpine --mount type=bind,source=/wd,destination=/wd,readonly |
grep --quiet "^docker.orig --host test run --mount type=bind,src=${fixtures_dir},target=/wd,readonly --mount type=bind,source=/container-root/test,dst=/test,readonly --mount type=bind,source=${fixtures_dir}/testfile,dst=/test/testfile,readonly alpine --mount type=bind,source=/wd,destination=/wd,readonly$"

echo "Same but for container run"
"${docker_args[@]}" --env DOND_SHIM_PRINT_COMMAND=true --volume "${fixtures_dir}:/wd" "${image_id}" \
docker --host test container run --volume /wd:/wd alpine --volume /wd:/wd |
grep --quiet "^docker.orig --host test container run --volume ${fixtures_dir}:/wd alpine --volume /wd:/wd$"

echo "Same but for create"
"${docker_args[@]}" --env DOND_SHIM_PRINT_COMMAND=true --volume "${fixtures_dir}:/wd" "${image_id}" \
docker --host test create --volume /wd:/wd alpine --volume /wd:/wd |
grep --quiet "^docker.orig --host test create --volume ${fixtures_dir}:/wd alpine --volume /wd:/wd$"

echo "Same but container create"
"${docker_args[@]}" --env DOND_SHIM_PRINT_COMMAND=true --volume "${fixtures_dir}:/wd" "${image_id}" \
docker --host test container create --volume /wd:/wd alpine --volume /wd:/wd |
grep --quiet "^docker.orig --host test container create --volume ${fixtures_dir}:/wd alpine --volume /wd:/wd$"

echo "Do not do anything for other commands"
"${docker_args[@]}" --env DOND_SHIM_PRINT_COMMAND=true --volume "${fixtures_dir}:/wd" "${image_id}" \
docker --host test whatever --volume /wd:/wd alpine --volume /wd:/wd |
grep --quiet "^docker.orig --host test whatever --volume /wd:/wd alpine --volume /wd:/wd$"

echo "Check if docker on docker is working"
"${docker_args[@]}" "${image_id}" \
docker version >/dev/null

echo "Check if mounting an volume from the container gets fixed"
"${docker_args[@]}" "${image_id}" \
docker run --rm --volume /test/only-inside-container:/only-inside-container ubuntu:latest grep "^test$" /only-inside-container >/dev/null

echo "Same but with equals sign"
"${docker_args[@]}" "${image_id}" \
docker run --rm --volume=/test/only-inside-container:/only-inside-container ubuntu:latest grep "^test$" /only-inside-container >/dev/null

echo "Check if mounting a volume which is already a volume gets fixed"
"${docker_args[@]}" --volume "${fixtures_dir}:/wd" "${image_id}" \
# echo "Do not change global options or after the image"
# "${docker_args[@]}" --env DOND_SHIM_PRINT_COMMAND=true --volume "${fixtures_dir}:/wd" "${image_id}" \
# docker --host test run --volume /wd:/wd alpine --volume /wd:/wd |
# grep --quiet "^docker.orig --host test run --volume ${fixtures_dir}:/wd alpine --volume /wd:/wd$"

# echo "Same as above, but retaining read only mode"
# "${docker_args[@]}" --env DOND_SHIM_PRINT_COMMAND=true --volume "${fixtures_dir}:/wd" "${image_id}" \
# docker --host test run --volume /wd:/wd:ro alpine --volume /wd:/wd |
# grep --quiet "^docker.orig --host test run --volume ${fixtures_dir}:/wd:ro alpine --volume /wd:/wd$"

# echo "Same as above but with --mount"
# "${docker_args[@]}" --env DOND_SHIM_PRINT_COMMAND=true --volume "${fixtures_dir}:/wd" "${image_id}" \
# docker --host test run --volume /wd:/wd:ro --mount=type=bind,source=/wd,readonly,destination=/wd2 alpine --volume /wd:/wd |
# grep --quiet "^docker.orig --host test run --volume ${fixtures_dir}:/wd:ro --mount=type=bind,source=${fixtures_dir},readonly,destination=/wd2 alpine --volume /wd:/wd$"

# echo "Same as above (without --mount), but retaining read only mode on auto added volume"
# "${docker_args[@]}" --env DOND_SHIM_PRINT_COMMAND=true --env DOND_SHIM_MOCK_CONTAINER_ROOT_ON_HOST=/container-root --volume "${fixtures_dir}:/wd" --volume "${fixtures_dir}/testfile:/test/testfile" "${image_id}" \
# docker --host test run --volume /wd:/wd:ro --volume /test:/test:ro alpine --volume /wd:/wd |
# grep --quiet "^docker.orig --host test run --volume ${fixtures_dir}:/wd:ro --volume /container-root/test:/test:ro --volume ${fixtures_dir}/testfile:/test/testfile:ro alpine --volume /wd:/wd$"

# echo "Same as above but should not auto add mounts which are not bind mounts"
# "${docker_args[@]}" --env DOND_SHIM_PRINT_COMMAND=true --env DOND_SHIM_MOCK_CONTAINER_ROOT_ON_HOST=/container-root --volume "${fixtures_dir}:/wd" --volume "${fixtures_dir}/testfile:/test/testfile" --mount type=tmpfs,target=/test/tmpfsdir "${image_id}" \
# docker --host test run --volume /wd:/wd:ro --volume /test:/test:ro alpine --volume /wd:/wd |
# grep --quiet "^docker.orig --host test run --volume ${fixtures_dir}:/wd:ro --volume /container-root/test:/test:ro --volume ${fixtures_dir}/testfile:/test/testfile:ro alpine --volume /wd:/wd$"

# echo "Same as above (with --mount src and target, dst), but retaining read only mode on auto added volume"
# "${docker_args[@]}" --env DOND_SHIM_PRINT_COMMAND=true --env DOND_SHIM_MOCK_CONTAINER_ROOT_ON_HOST=/container-root --volume "${fixtures_dir}:/wd" --volume "${fixtures_dir}/testfile:/test/testfile" "${image_id}" \
# docker --host test run --mount type=bind,src=/wd,target=/wd,readonly --mount type=bind,source=/test,dst=/test,readonly alpine --mount type=bind,source=/wd,destination=/wd,readonly |
# grep --quiet "^docker.orig --host test run --mount type=bind,src=${fixtures_dir},target=/wd,readonly --mount type=bind,source=/container-root/test,dst=/test,readonly --mount type=bind,source=${fixtures_dir}/testfile,dst=/test/testfile,readonly alpine --mount type=bind,source=/wd,destination=/wd,readonly$"

# echo "Same but for container run"
# "${docker_args[@]}" --env DOND_SHIM_PRINT_COMMAND=true --volume "${fixtures_dir}:/wd" "${image_id}" \
# docker --host test container run --volume /wd:/wd alpine --volume /wd:/wd |
# grep --quiet "^docker.orig --host test container run --volume ${fixtures_dir}:/wd alpine --volume /wd:/wd$"

# echo "Same but for create"
# "${docker_args[@]}" --env DOND_SHIM_PRINT_COMMAND=true --volume "${fixtures_dir}:/wd" "${image_id}" \
# docker --host test create --volume /wd:/wd alpine --volume /wd:/wd |
# grep --quiet "^docker.orig --host test create --volume ${fixtures_dir}:/wd alpine --volume /wd:/wd$"

# echo "Same but container create"
# "${docker_args[@]}" --env DOND_SHIM_PRINT_COMMAND=true --volume "${fixtures_dir}:/wd" "${image_id}" \
# docker --host test container create --volume /wd:/wd alpine --volume /wd:/wd |
# grep --quiet "^docker.orig --host test container create --volume ${fixtures_dir}:/wd alpine --volume /wd:/wd$"

# echo "Do not do anything for other commands"
# "${docker_args[@]}" --env DOND_SHIM_PRINT_COMMAND=true --volume "${fixtures_dir}:/wd" "${image_id}" \
# docker --host test whatever --volume /wd:/wd alpine --volume /wd:/wd |
# grep --quiet "^docker.orig --host test whatever --volume /wd:/wd alpine --volume /wd:/wd$"

# echo "Check if docker on docker is working"
# "${docker_args[@]}" "${image_id}" \
# docker version >/dev/null

# echo "Check if mounting an volume from the container gets fixed"
# "${docker_args[@]}" "${image_id}" \
# docker run --rm --volume /test/only-inside-container:/only-inside-container ubuntu:latest grep "^test$" /only-inside-container >/dev/null

# echo "Same but with equals sign"
# "${docker_args[@]}" "${image_id}" \
# docker run --rm --volume=/test/only-inside-container:/only-inside-container ubuntu:latest grep "^test$" /only-inside-container >/dev/null

# echo "Check if mounting a volume which is already a volume gets fixed"
# "${docker_args[@]}" --volume "${fixtures_dir}:/wd" "${image_id}" \
# docker run --rm --volume /wd:/wd ubuntu:latest grep "^test$" /wd/testfile >/dev/null

# echo "Same as above but for a file within the volume"
# "${docker_args[@]}" --volume "${fixtures_dir}:/wd" "${image_id}" \
# docker run --rm --volume /wd/testfile:/wd/testfile ubuntu:latest grep "^test$" /wd/testfile >/dev/null

# echo "Check if mounting a volume which contains another volume adds all proper volumes"
# "${docker_args[@]}" --volume "${fixtures_dir}/testfile:/test/testfile" "${image_id}" \
# docker run --rm --volume /test:/wd ubuntu:latest grep "^test$" /wd/testfile >/dev/null

# echo "With --mount"
# "${docker_args[@]}" --volume "${fixtures_dir}/testfile:/test/testfile" "${image_id}" \
# docker run --rm --mount type=bind,source=/test,destination=/wd ubuntu:latest grep "^test$" /wd/testfile >/dev/null

# echo "With --mount shuffling order"
# "${docker_args[@]}" --volume "${fixtures_dir}/testfile:/test/testfile" "${image_id}" \
# docker run --rm --mount destination=/wd,source=/test,type=bind ubuntu:latest grep "^test$" /wd/testfile >/dev/null

# echo "Same as above but for multiple files under different volumes"
# "${docker_args[@]}" --volume "${fixtures_dir}/testfile:/test/testfile" --volume "${fixtures_dir}/testfile:/test/testfile2" "${image_id}" \
# docker run --rm --volume /test:/wd ubuntu:latest bash -c 'grep "^test$" /wd/testfile && grep "^test$" /wd/testfile2 && grep "^test$" /wd/only-inside-container' >/dev/null

# echo "Same test as above but with a read only volume"
# "${docker_args[@]}" --volume "${fixtures_dir}/testfile:/test/testfile" --volume "${fixtures_dir}/testfile:/test/testfile2" "${image_id}" \
# docker run --rm --volume /test:/wd:ro ubuntu:latest bash -c 'grep "^test$" /wd/testfile && grep "^test$" /wd/testfile2 && grep "^test$" /wd/only-inside-container' >/dev/null

# echo "Same as above but with a volume that matches the parent first"
# "${docker_args[@]}" --volume "${fixtures_dir}:/folder" --volume "${fixtures_dir}/testfile:/test/testfile" --volume "${fixtures_dir}/testfile:/test/testfile2" "${image_id}" \
# docker run --rm --volume /folder:/test --volume /test:/wd:ro ubuntu:latest bash -c 'grep "^test$" /wd/testfile && grep "^test$" /wd/testfile2 && grep "^test$" /wd/only-inside-container' >/dev/null

echo "With named volume"
volume_name=$(docker volume create --opt type=none --opt device="${fixtures_dir}" --opt o=bind)
trap 'docker volume rm -f "${volume_name}" >/dev/null' EXIT
# this confirms the volume works outside of the shim
"${docker_args[@]}" --volume "${volume_name}:/wd" "${image_id}" \
grep "^test$" /wd/testfile >/dev/null
# this confirms the volume works inside of the shim
"${docker_args[@]}" --volume "${volume_name}:/wd" "${image_id}" \
docker run --rm --volume /wd:/wd ubuntu:latest grep "^test$" /wd/testfile >/dev/null

echo "Same as above but for a file within the volume"
"${docker_args[@]}" --volume "${fixtures_dir}:/wd" "${image_id}" \
docker run --rm --volume /wd/testfile:/wd/testfile ubuntu:latest grep "^test$" /wd/testfile >/dev/null

echo "Check if mounting a volume which contains another volume adds all proper volumes"
"${docker_args[@]}" --volume "${fixtures_dir}/testfile:/test/testfile" "${image_id}" \
docker run --rm --volume /test:/wd ubuntu:latest grep "^test$" /wd/testfile >/dev/null

echo "With --mount"
"${docker_args[@]}" --volume "${fixtures_dir}/testfile:/test/testfile" "${image_id}" \
docker run --rm --mount type=bind,source=/test,destination=/wd ubuntu:latest grep "^test$" /wd/testfile >/dev/null

echo "With --mount shuffling order"
"${docker_args[@]}" --volume "${fixtures_dir}/testfile:/test/testfile" "${image_id}" \
docker run --rm --mount destination=/wd,source=/test,type=bind ubuntu:latest grep "^test$" /wd/testfile >/dev/null

echo "Same as above but for multiple files under different volumes"
"${docker_args[@]}" --volume "${fixtures_dir}/testfile:/test/testfile" --volume "${fixtures_dir}/testfile:/test/testfile2" "${image_id}" \
docker run --rm --volume /test:/wd ubuntu:latest bash -c 'grep "^test$" /wd/testfile && grep "^test$" /wd/testfile2 && grep "^test$" /wd/only-inside-container' >/dev/null

echo "Same test as above but with a read only volume"
"${docker_args[@]}" --volume "${fixtures_dir}/testfile:/test/testfile" --volume "${fixtures_dir}/testfile:/test/testfile2" "${image_id}" \
docker run --rm --volume /test:/wd:ro ubuntu:latest bash -c 'grep "^test$" /wd/testfile && grep "^test$" /wd/testfile2 && grep "^test$" /wd/only-inside-container' >/dev/null

echo "Same as above but with a volume that matches the parent first"
"${docker_args[@]}" --volume "${fixtures_dir}:/folder" --volume "${fixtures_dir}/testfile:/test/testfile" --volume "${fixtures_dir}/testfile:/test/testfile2" "${image_id}" \
docker run --rm --volume /folder:/test --volume /test:/wd:ro ubuntu:latest bash -c 'grep "^test$" /wd/testfile && grep "^test$" /wd/testfile2 && grep "^test$" /wd/only-inside-container' >/dev/null
done

0 comments on commit ddd9487

Please sign in to comment.