pklndnst
pklndnst

Reputation: 756

Docker's behavior when mounting volumes

I'm currently trying to understand how Docker handles mounting of volumes and ran into the following behavior which seems kind of stange to me:

Assumed that we want to mount the /var/run directory into a container (just as an example), we do the following:

$ docker run -i -t -v /var/run:/test ubuntu:latest /bin/bash

So far, everything works fine and all the folders and files residing under /var/run show up inside the container within /test.

Now see what happens if we decide to mount the /var directory:

$ docker run -i -t -v /var:/test ubuntu:latest /bin/bash

Still, all the host folders within /var show up inside /test. However, after cd into /test/run, the files and directories from the host are not displayed. In other words, Docker not seems to do a 'recursive' mount of subsequent child directories and their content. Is this ordinary Docker behavior?

Upvotes: 6

Views: 2489

Answers (1)

larsks
larsks

Reputation: 311750

That's not just ordinary Docker behavior; that's ordinary linux behavior. When you bind-mount a filesystem onto another directory, as in:

mkdir /tmp/mount
mount -o bind /var /tmp/mount

You will only see files in the destination mount that exist in the source filesystem. You will not see the files contained in any child mounts unless you were to explicitly bind mount those directories as well:

mount -o bind /var/run /tmp/mount/run

This is exactly the behavior you see with Docker because this is exactly the same mechanism Docker uses to expose host directories inside your containers.

Upvotes: 9

Related Questions