title: What happened when I use a host volume(bind mounts) and named volume(one type of docker managed volume) at the same time?
body: <p>I have read the some documents before asking this question:</p>
  
  <ul>
  <li><a href="https://docs.docker.com/glossary/?term=volume" rel="nofollow noreferrer">https://docs.docker.com/glossary/?term=volume</a></li>
  <li><a href="https://docs.docker.com/storage/" rel="nofollow noreferrer">https://docs.docker.com/storage/</a></li>
  <li><a href="https://docs.docker.com/storage/volumes/" rel="nofollow noreferrer">https://docs.docker.com/storage/volumes/</a></li>
  <li><a href="https://docs.docker.com/storage/bind-mounts/" rel="nofollow noreferrer">https://docs.docker.com/storage/bind-mounts/</a></li>
  </ul>
  
  <p>And I am confused about the following two parts:</p>
  
  <ol>
  <li>bind mounts: <a href="https://docs.docker.com/storage/bind-mounts/#mounting-into-a-non-empty-directory-on-the-container" rel="nofollow noreferrer">Mounting into a non-empty directory on the container</a></li>
  <li>named volume: <a href="https://docs.docker.com/storage/volumes/#populate-a-volume-using-a-container" rel="nofollow noreferrer">Populate a volume using a container</a></li>
  </ol>
  
  <p>Their behavior is completely opposite:</p>
  
  <p>For bind mounts: </p>
  
  <blockquote>
    <p>If you bind-mount into a non-empty directory on the container, the directory’s existing contents are obscured by the bind mount.</p>
  </blockquote>
  
  <p>For named volume and anonymous volumes:</p>
  
  <blockquote>
    <p>If you start a container which creates a new volume, as above, and the container has files or directories in the directory to be mounted (such as /app/ above), the directory’s contents are copied into the volume. The container then mounts and uses the volume, and other containers which use the volume also have access to the pre-populated content.</p>
  </blockquote>
  
  <p>My question is, if I use a <code>VOLUME</code> command in dockerfile to create a <del>named volume</del> anonymous volumes, and at the same time, use a bind mount to mount a non exist path into the container, what goes on behind the scenes?</p>
  
  <p>For example:</p>
  
  <pre><code># in docker file
  VOLUME /path/in/container
  
  # when run container
  docker run -v /not-exist-dir/in/host:/path/in/container  ... /&lt; image &gt;
  </code></pre>
  
  <p>I have do some test and the result is:</p>
  
  <ol>
  <li>no anonymous volumes was created in <code>/var/lib/docker/volumes/</code></li>
  <li>the not-exist-dir was created on the host and it is not empty now, I think the <a href="https://docs.docker.com/storage/volumes/#populate-a-volume-using-a-container" rel="nofollow noreferrer">Populate a volume using a container</a> has take effects.</li>
  </ol>
  
  <p>So,</p>
  
  <ol>
  <li>why there is no anonymous volumes created as expected?</li>
  <li>why the directory’s existing contents(in container side) are not obscured by the bind mount?</li>
  </ol>
  
  <p>what happened behind this scenario?</p>
  
  <hr>
  
  <p>Let me give a more specific example:</p>
  
  <p>Here is the <a href="https://hub.docker.com/r/openfrontier/gerrit/dockerfile" rel="nofollow noreferrer">dockerfile of the openfrontier/gerrit image</a>, we can see at the end of the docker file, there is a VOLUME command:</p>
  
  <pre><code>VOLUME $GERRIT_SITE
  </code></pre>
  
  <p>which will actually create an anonymous volumes on the host and mount it to <code>/var/gerrit/review_site</code>(value of GERRIT_SITE) in the container when any container is created from this image like this:</p>
  
  <pre><code>docker run -dit --name gerrit  openfrontier/gerrit:2.15.3
  </code></pre>
  
  <p>after that, I can see the anonymous volumes under <code>/var/lib/docker/volumes/</code> and I can use <code>docker volume ls</code> to see its name <code>be4538dbf3a51da463391c6eca9714fb6dd0c11379f1e2918f74c33d56633f00</code>, and also with <code>docker inspect gerrit</code> command, we can see:</p>
  
  <pre><code>"Mounts": [
              {
                  "Type": "volume",
                  "Name": "be4538dbf3a51da463391c6eca9714fb6dd0c11379f1e2918f74c33d56633f00",
                  "Source": "/var/lib/docker/volumes/be4538dbf3a51da463391c6eca9714fb6dd0c11379f1e2918f74c33d56633f00/_data",
                  "Destination": "/var/gerrit/review_site",
                  "Driver": "local",
                  "Mode": "",
                  "RW": true,
                  "Propagation": ""
              }
          ],
  
  </code></pre>
  
  <p>and there are some file under these two folder, which are created by the container after it running.</p>
  
  <p>Till now, this is a normal example for create an anonymous volumes by using VOLUME command.</p>
  
  <p>However, if I run this container with the following command:</p>
  
  <pre><code>docker run -dit --name gerrit -v /home/test:/var/gerrit/review_site openfrontier/gerrit:2.15.3
  </code></pre>
  
  <p>where <code>/home/test</code> is not exist on the host, then no anonymous volumes is created, instead, <code>/home/test</code> folder is created and it is not empty!!!, the mount info is as follows:</p>
  
  <pre><code>"Mounts": [
              {
                  "Type": "bind",
                  "Source": "/home/test",
                  "Destination": "/var/gerrit/review_site",
                  "Mode": "",
                  "RW": true,
                  "Propagation": "rprivate"
              }
          ],
  </code></pre>
  
  <p>I think bind mounts is take effects here but I just do not understand why the path on the host is not empty(since bind-mount into a non-empty directory on the container, the directory’s existing contents are obscured by the bind mount.)</p>
  
  <p>I also test mounting a not empty folder to the container by put some file under "/home/test" before I use <code>docker run</code> to start a container, the result is the original file was kept and new files was added too.</p>
  
  <p>I know the examples above are not good practice to use docker volume, But I just wondering what happened behind. </p>