Skip to content

Instantly share code, notes, and snippets.

@Drowze
Last active May 17, 2021 11:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Drowze/c07c7acc5ed42f358e82798bb488ca09 to your computer and use it in GitHub Desktop.
Save Drowze/c07c7acc5ed42f358e82798bb488ca09 to your computer and use it in GitHub Desktop.
docker in docker, issues with mounting a volume

Let's try to use volumes on a docker-in-docker setup!

  • First start docker in docker
$ docker pull docker
$ docker run --rm -it -v /var/run/docker.sock:/var/run/docker.sock --name docker-outer docker sh
  • Then start an inner container, create a file there and copy it to the outer container
docker run --entrypoint sh --name docker-inner alpine -c "echo 'hello world' > /test-file"
docker cp docker-inner:/test-file /test-file
cat /test-file
hello world # the file exists :-)
  • Finally, try to start another inner container, mounting something from the outer container to the new inner container
    At first I try to mount the file from the root (/) path, which errors out on mac (but not necessarily on linux). this happens because, since we are mounting the docker socket to run docker containers inside a docker container, inner containers are actually "sibling" containers, so any mounted volume on an inner container will be mounting from the host filesystem instead of the outer container filesystem (and on MacOS we do not have write access to /, which is not necessarily true on a linux system)
$ docker run --rm -it -v /test-file:/test-file --name docker-inner2 alpine sh
docker: Error response from daemon: Mounts denied:
The path /test-file is not shared from the host and is not known to Docker.
You can configure shared paths from Docker -> Preferences... -> Resources -> File Sharing.
See https://docs.docker.com/docker-for-mac for more info.
ERRO[0000] error waiting for container: context canceled
  • Now let's try to mount the file from the a path that is the same to a path that we have write access on the host (Drowze is just my username)
$ mkdir /Users/Drowze && cp /test-file /Users/Drowze
  • Then we try to mount it and bam!
$ docker run --rm -it -v /Users/Drowze/test-file:/test-file --name docker-inner2 alpine sh
$ cat /test-file
cat: read error: Is a directory # since the file doesn't exist on the host... there you go an empty directory!

We can solve that using data volumes instead!

$ docker run --rm -it -v /var/run/docker.sock:/var/run/docker.sock --name docker-outer docker sh
$ docker volume create shared_files

$ docker run --rm --entrypoint sh --mount source=shared_files,target=/shared_files alpine -c "echo 'hello world' > /shared_files/test-file"
$ docker run --rm --entrypoint sh --mount source=shared_files,target=/shared_files alpine -c "cat /shared_files/test-file"
hello world # it works! we have a orhpan `shared_files` volume containing our shared data, which we can mount in any container
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment