What happens when a volume links an existing populated host and container dir

Whenever a Docker container is created with a volume mounted on the host, e.g.:

docker run -v /path/on/host:/data container-image

Any contents that are already in /data due to the image build process are always completely discarded, and whatever is currently at /path/on/host is used in its place. (If /path/on/host does not exist, it is created as an empty directory, though I think some aspect of that behavior may currently be deprecated.)

Pre-defining a volume in the Dockerfile with VOLUME is not necessary; all VOLUME does is cause any containers run from the image to have an implicit -v /volume/path (Note lack of host mount path) argument added to their docker run command which is ignored if an explicit -v /host/path:/volume/path is used.


When you run a container and mount a volume from the host, all you see in the container is what is on the host - the volume mount points at the host directory, so if there was anything in the directory in the image it gets bypassed.

With an image from this Dockerfile:

FROM ubuntu
WORKDIR /vol
RUN touch /vol/from-container
VOLUME /vol

When you run it without a host mount, the image contents get copied into the volume:

> docker run vol-test ls /vol
from-container 

But mount the volume from the host and you only see the host's content:

> ls $(pwd)/host
from-host
> docker run -v $(pwd)/host:/vol vol-test ls /vol
from-host

And no, you don't need the VOLUME instruction. The behaviour is the same without it.