Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I've searched the docs but nothing came up so time to test it. But for a quick future reference...

Is the host folder populated with the container folder contents?

Is it the opposite?

Are both folder contents merged? (In that case: What happens when a file with the same name is in both folders?)

Or does it produce an error? Is the error thrown on launch or is it thrown when you try to build an image with a VOLUME pointing to an existing populated folder on the container?

Also, another thing that isn't in the docs: Do I have to define the container path as a VOLUME in the Dockerfile in order to use -v against it when launching the container or can I create volumes on the fly?

like image 439
NotGaeL Avatar asked Sep 22 '16 19:09

NotGaeL


People also ask

Can a Docker volume be shared between containers?

Persistent access to data is provided with Docker Volumes. Docker Volumes can be created and attached in the same command that creates a container, or they can be created independently of any containers and attached later. In this article, we'll look at four different ways to share data between containers.

How do I mount a volume to an already running container?

To attach a volume into a running container, we are going to: use nsenter to mount the whole filesystem containing this volume on a temporary mountpoint; create a bind mount from the specific directory that we want to use as the volume, to the right location of this volume; umount the temporary mountpoint.

Can multiple containers mount same volume?

Share Data with Volumes. Multiple containers can run with the same volume when they need access to shared data. Docker creates a local volume by default.


1 Answers

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.

like image 122
Elton Stoneman Avatar answered Oct 30 '22 16:10

Elton Stoneman