title: How docker handles multiple mount types?
body: <p>This post is somewhat  lenglthy but bear with me for a while...</p>
  <p>Suppose you have an app that sits in <code>/app</code> in your local (host) filesystem with the following structure</p>
  <pre><code>app
  |-- index.php
  |-- foo
  |   `-- file-h1
  `-- bar
      `-- file-h2
  </code></pre>
  <p>Now suppose we have an image (tagged <code>myrepo/app</code>) that exploits the following data structure</p>
  <pre><code>opt
  |-- app
  |   `-- foo
  |       `-- file-c1
  </code></pre>
  <p>If we run a container from that image by mounting host's <code>/app</code> to container's <code>/opt/app</code> as follows</p>
  <pre><code>docker container run \
  -v /app:/opt/app \
  myrepo/app
  </code></pre>
  <p>The resulting data structure of the container will be the following</p>
  <pre><code>opt
  |-- app
  |   |-- index.php
  |   |-- foo       
  |   |   `-- file-h1          
  |   `-- bar
  |       `-- file-h2 
  </code></pre>
  <p>So far, so good...</p>
  <h3>Running container with multiple mounts (both bind-mounts and volumes)</h3>
  <p>Now, lets say that we want to use <strong>both</strong> a <strong>named volume</strong> called <code>data</code> to be mounted on <code>/opt/app/foo</code> and a <strong>bind-mount</strong> for mounting <code>/app</code> to <code>/opt/app</code></p>
  <pre><code>docker container run \
  -v /app:/opt/app \
  -v data:/opt/app/foo
  myrepo/app
  </code></pre>
  <p>The resulting data structure inside the container will be:</p>
  <pre><code>opt
  |-- app
  |   |-- index.php
  |   |-- foo       
  |   |   `-- file-c1          
  |   `-- bar
  |       `-- file-h2 
  </code></pre>
  <p>As it is stated in various posts (like <a href="https://github.com/moby/moby/issues/26051#issuecomment-242847347" rel="nofollow noreferrer">this</a> and <a href="https://github.com/moby/moby/issues/19120#issuecomment-169330570" rel="nofollow noreferrer">this</a>) 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 (<code> -v /app:/opt/app</code>) and then the volume (<code>-v data:/opt/app/foo</code>).</p>
  <p>Hence, I would <em>expect</em> that the contents of host's <code>/app</code> would replace/obscure the contents of container's <code>/opt/app</code> and thus <code>file-h1</code> to be inside <code>/opt/app/foo</code>. Finally, <code>file-h1</code> would be <a href="https://docs.docker.com/engine/admin/volumes/#tips-for-using-bind-mounts-or-volumes" rel="nofollow noreferrer">copied</a> in the newly created <code>data</code> volume and the volume would be mounted on <code>/opt/app/foo</code> (so <code>file-h1</code> should be shown instead of <code>file-c1</code> )</p>
  <p>My questions raised when I tried to understand <a href="https://stackoverflow.com/a/38601156/2235814">this</a> answer on SO</p>