Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 451
pklndnst Avatar asked Aug 09 '16 15:08

pklndnst


People also ask

What is an ideal use case for Docker's bind mount storage?

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.

Can data volumes can be mounted in read only mode?

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.


1 Answers

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.

like image 99
larsks Avatar answered Sep 19 '22 13:09

larsks