Docker Community Forums

Share and learn in the Docker community.

Volume Mounting - Under the Hood (also how to connect to your docker daemon from a container)

(M4dc4p) #1

So I was trying to connect to the docker daemon from inside a container. Could not figure it out - using TCP didn’t work. Then I found a blog post ( that gave the following command:

docker run -it -v /var/run/docker.sock:/var/run/docker.sock -v /usr/bin/docker:/usr/bin/docker alpine sh

(try it; running “docker info” and “docker images” will show info from your Windows host).

But how does it work? Why can I mount /var/run/docker.sock, when clearly that doesn’t exists on my Windows filesystem?

The clue is MobyLinuxVM - the Linux VM installed by Docker for Windows that uses the native Hyper-V hypervisor. I think the docker daemon is actually running in that VM, which the Docker for Windows client talks to. All volume mounting commands actually refer to paths in the VM, rather than on the native Windows filesystem. For example, I’ve noticed that mounting (1.12.1-beta26) requires that I use forward slashes:

docker run -it -v C:/:/cdrive alpine sh

(running the above mounts C:\ at /cdrive in the container). But I can also write:

docker run -it -v /C:/cdrive alpine sh

which has the same effect.

Why? Well, we can explore the MobyLinuxVM’s filesystem by mounting its root directory in a container:

docker run -it -v //:/moby alpine sh / # ls -l /moby/ total 24 drwxr-xr-x 2 root root 8192 Sep 14 20:37 C drwxrwxr-x 1 1001 50 0 Jan 1 1970 Database drwxr-xr-x 2 root root 1760 Sep 8 21:29 bin drwxr-xr-x 2 root root 8192 Sep 14 20:37 c ...

And now you can see why the /C syntax works - the MobyLinuxVM just mounts your local drives in the root directory, using upper and lowercase names. (Notably, if you choose to not share these drives in the Docker for Windows app, then the C and c mounts go away).

SO that’s why I can share the docker socket and docker binary in my container - I’m actually mounting them from the MobyLinuxVM’s filesystem!