You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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)
$ 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