Let's take an example.
The following is the VOLUME
instruction for the nginx
image:
VOLUME ["/etc/nginx/sites-enabled", "/etc/nginx/certs", "/etc/nginx/conf.d", "/var/log/nginx", "/var/www/html"]
Here are my questions:
When you start the container, will these directories show up on my host? And when I stop my container, the directories will stay?
If some (or all) of these directories already exist in my host, what will happen? For example, let's say the image comes with a default config file within the
/etc/nginx
directory of the container, and I also have a config file within/etc/nginx
on my host. When the container starts, which of these files will get priority?What's the key difference between
-v /host/dir:container/dir
andVOLUME
?
References:
A container's volumes are just directories on the host regardless of what method they are created by. If you don't specify a directory on the host, Docker will create a new directory for the volume, normally under
/var/lib/docker/vfs
.However the volume was created, it's easy to find where it is on the host by using the
docker inspect
command e.g:We can see that Docker has created a directory for the volume at
/var/lib/docker/volumes/0d7adb21591798357ac1e140735150192903daf3de775105c18149552a26f951/_data
.You are free to modify/add/delete files in this directory from the host, but note that you may need to use
sudo
for permissions.Docker will only delete volume directories in two circumstances:
--rm
option is given todocker run
, any volumes will be deleted when the container exitsdocker rm -v CONTAINER
, any volumes will be removed.In both cases, volumes will only be deleted if no other containers refer to them. Volumes mapped to specific host directories (the
-v HOST_DIR:CON_DIR
syntax) are never deleted by Docker. However, if you remove the container for a volume, the naming scheme means you will have a hard time figuring out which directory contains the volume.So, specific questions:
VOLUME
instruction is identical to-v
without specifying the host dir. When the host dir is specified, Docker does not create any directories for the volume, will not copy in files from the image and will never delete the volume (docker rm -v CONTAINER
will not delete volumes mapped to user-specified host directories).More information here:
http://container-solutions.com/2014/12/understanding-volumes-docker/