The official docker tutorial says:
A data volume is a specially-designated directory within one or more containers that bypasses the Union File System. Data volumes provide several useful features for persistent or shared data:
Volumes are initialized when a container is created. If the container’s base image contains data at the specified mount point,
that existing data is copied into the new volume upon volume
initialization. (Note that this does not apply when mounting a host
directory.)Data volumes can be shared and reused among containers.
Changes to a data volume are made directly.
Changes to a data volume will not be included when you update an image.
Data volumes persist even if the container itself is deleted.
In Dockerfile
you can specify only the destination of a volume inside a container. e.g. /usr/src/app
.
When you run a container, e.g. docker run --volume=/opt:/usr/src/app my_image
, you may but do not have to specify its mounting point (/opt
) on the host machine. If you do not specify --volume
argument then the mount point will be chosen automatically, usually under /var/lib/docker/volumes/
.