Docker Community Forums

Share and learn in the Docker community.

How docker handles multiple mount types?

docker

(Iraklisg) #1

This post is somewhat lenglthy but bear with me for a while…

Suppose you have an app that sits in /app in your local (host) filesystem with the following structure

app
|-- index.php
|-- foo
|   `-- file-h1
`-- bar
    `-- file-h2

Now suppose we have an image (tagged myrepo/app) that exploits the following data structure

opt
|-- app
|   `-- foo
|       `-- file-c1

If we run a container from that image by mounting host’s /app to container’s /opt/app as follows

docker container run \
-v /app:/opt/app \
myrepo/app

The resulting data structure of the container will be the following

opt
|-- app
|   |-- index.php
|   |-- foo       
|   |   `-- file-h1          
|   `-- bar
|       `-- file-h2 

So far, so good…

Running container with multiple mounts (both bind-mounts and volumes)

Now, lets say that we want to use both a named volume called data to be mounted on /opt/app/vendor and a bind-mount to mount /app to /opt/app

docker container run \
-v /app:/opt/app \
-v data:/opt/app/foo
myrepo/app

The resulting data structure inside the container will be:

opt
|-- app
|   |-- index.php
|   |-- foo       
|   |   `-- file-c1          
|   `-- bar
|       `-- file-h2 

As it is stated in various posts (like this and this) the docker mounts are performed in lexicographic order (i.e shortest path first). According to this, I would expect docker first to execute the bind-mount (-v /app:/opt/app) and then the volume (-v data:/opt/app/foo).

Hence, I would expect that the contents of host’s /app would replace/obscure the contents of container’s /opt/app and thus file-h1 to be inside /opt/app/foo. Finally, file-h1 would be copied in the newly created data volume and the volume would be mounted on /opt/app/foo (so file-h1 should be shown instead of file-c1 )

My questions raised when I tried to understand this answer on SO