I have read the some documents before asking this question:
And I am confused about the following two parts:
- bind mounts: Mounting into a non-empty directory on the container
- named volume: Populate a volume using a container
Their behavior is completely opposite:
For bind mounts:
If you bind-mount into a non-empty directory on the container, the directory’s existing contents are obscured by the bind mount.
For named volume:
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.
My question is, if I use a
VOLUME command in dockerfile to create a named volume, and at the same time, use a bind mount to mount a non exist path into the container, what goes on behind the scenes?
# in docker file VOLUME /path/in/container # when run container docker run -v /not-exist-dir/in/host:/path/in/container ... /< image >
I have do some test and the result is:
- no anonymous volumes was created in
- the not-exist-dir was created on the host and it is not empty now, I think the Populate a volume using a container has take effects.
- why there is no anonymous volumes created as expected?
- why the directory’s existing contents(in container side) are not obscured by the bind mount?
what happened behind this scenario?