From ddd9487407d0b188eeca4eb5627b9be2fe03e4fc Mon Sep 17 00:00:00 2001 From: Felipe Santos Date: Wed, 16 Oct 2024 19:37:52 -0300 Subject: [PATCH] Add support for named volumes Closes #13 --- dond | 2 +- scripts/test.sh | 194 +++++++++++++++++++++++++----------------------- 2 files changed, 103 insertions(+), 93 deletions(-) diff --git a/dond b/dond index f3b11a9..e38eeba 100755 --- a/dond +++ b/dond @@ -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}" ) diff --git a/scripts/test.sh b/scripts/test.sh index 1222932..732bc3e 100755 --- a/scripts/test.sh +++ b/scripts/test.sh @@ -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