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?
Good use cases for bind mounts Bind mounts are appropriate for the following types of use case: Sharing configuration files from the host machine to containers. This is how Docker provides DNS resolution to containers by default, by mounting /etc/resolv. conf from the host machine into each container.
Remember that multiple containers can mount the same volume, and it can be mounted read-write for some of them and read-only for others, at the same time.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With