Docker – Mounting a volume with docker in docker

docker

I'm running docker within a container using the command below:
docker run --rm -ti --privileged -v /var/run/docker.sock:/var/run/docker.sock docker:1.12 sh

Within that container's shell I see a directory with a bunch of files. Let's say /project with a filebuild.gradle. I'd like to start a new container and mount this directory using the command below

docker run -ti --rm -v /project:/project openjdk:8-jdk bash -c "ls /project".

However it looks like the project directory is empty, the ls /project does not show any files..

Now trying to mount a file:

docker run -ti --rm -v /project/build.gradle:/project/build.gradle openjdk:8-jdk bash -c "ls /project/"

This time the folder shows the file.

Anyone who has an idea why the volume mount does not work as expected. Is this a permission issue?

Best Answer

Nope. Inside the "docker" container, you only get a Docker client, not the whole thing. Once you mount the Docker socket inside your "docker" container, when you execute Docker commands, they go straight to the Docker "server" running on your actual Docker host. As such, that folder is expected to exist in your Docker Host.

Example how to mount a volume with docker in docker:

host$ pwd
/var/my-project

host$ docker run -v /var/run/docker.sock:/var/run/docker.sock -v $PWD:/app docker -it sh

/ # ls -l /app ### inside first docker
<LIST OF PROJECT FILES>

/ # docker run -v /var/my-project:/inception docker sh ### you need to pass HOST directory NOT currently running (/app) container directory

/ # ls -l /inception ### inside second docker
<LIST OF PROJECT FILES>
Related Topic