Use Volumes: Choose The - V or - Mount Flag
Use Volumes: Choose The - V or - Mount Flag
Volumes are the preferred mechanism for persisting data generated by and used by Docker
containers. While bind mounts are dependent on the directory structure of the host machine,
volumes are completely managed by Docker. Volumes have several advantages over bind mounts:
In addition, volumes are often a better choice than persisting data in a container’s writable layer,
because a volume does not increase the size of the containers using it, and the volume’s contents
exist outside the lifecycle of a given container.
If your container generates non-persistent state data, consider using a tmpfs mount to avoid storing
the data anywhere permanently, and to increase the container’s performance by avoiding writing into
the container’s writable layer.
Volumes use rprivate bind propagation, and bind propagation is not configurable for volumes.
-v or --volume: Consists of three fields, separated by colon characters (:). The fields must
be in the correct order, and the meaning of each field is not immediately obvious.
o In the case of named volumes, the first field is the name of the volume, and is unique
on a given host machine. For anonymous volumes, the first field is omitted.
o The second field is the path where the file or directory are mounted in the container.
o The third field is optional, and is a comma-separated list of options, such as ro.
These options are discussed below.
--mount: Consists of multiple key-value pairs, separated by commas and each consisting of
a <key>=<value> tuple. The --mount syntax is more verbose than -v or --volume, but the
order of the keys is not significant, and the value of the flag is easier to understand.
o The type of the mount, which can be bind, volume, or tmpfs. This topic discusses
volumes, so the type is always volume.
o The source of the mount. For named volumes, this is the name of the volume. For
anonymous volumes, this field is omitted. May be specified as source or src.
o The destination takes as its value the path where the file or directory is mounted in
the container. May be specified as destination, dst, or target.
o The readonly option, if present, causes the bind mount to be mounted into the
container as read-only.
o The volume-opt option, which can be specified more than once, takes a key-value
pair consisting of the option name and its value.
If your volume driver accepts a comma-separated list as an option, you must escape the value from
the outer CSV parser. To escape a volume-opt, surround it with double quotes (") and surround the
entire mount parameter with single quotes (').
For example, the local driver accepts mount options as a comma-separated list in the o parameter.
This example shows the correct way to escape the list.
$ docker service create \
--mount 'type=volume,src=<VOLUME-NAME>,dst=<CONTAINER-PATH>,volume-
driver=local,volume-opt=type=nfs,volume-opt=device=<nfs-server>:<nfs-path>,"volume-
opt=o=addr=<nfs-address>,vers=4,soft,timeo=180,bg,tcp,rw"'
--name myservice \
<IMAGE>
The examples below show both the --mount and -v syntax where possible, and --mount is presented
first.
Differences between -v and --mount behavior
As opposed to bind mounts, all options for volumes are available for both --mount and -v flags.
When using volumes with services, only --mount is supported.
Create a volume:
List volumes:
$ docker volume ls
local my-vol
Inspect a volume:
Remove a volume:
--mount
-v
$ docker run -d \
--name devtest \
--mount source=myvol2,target=/app \
nginx:latest
Use docker inspect devtest to verify that the volume was created and mounted correctly. Look for
the Mounts section:
"Mounts": [
{
"Type": "volume",
"Name": "myvol2",
"Source": "/var/lib/docker/volumes/myvol2/_data",
"Destination": "/app",
"Driver": "local",
"Mode": "",
"RW": true,
"Propagation": ""
}
],
This shows that the mount is a volume, it shows the correct source and destination, and that the
mount is read-write.
Stop the container and remove the volume. Note volume removal is a separate step.
Removing the service does not remove any volumes created by the service. Volume removal is a
separate step.
--mount
-v
$ docker run -d \
--name=nginxtest \
--mount source=nginx-vol,destination=/usr/share/nginx/html \
nginx:latest
After running either of these examples, run the following commands to clean up the containers and
volumes. Note volume removal is a separate step.
This example modifies the one above but mounts the directory as a read-only volume, by
adding ro to the (empty by default) list of options, after the mount point within the container. Where
multiple options are present, separate them by commas.
The --mount and -v examples have the same result.
--mount
-v
$ docker run -d \
--name=nginxtest \
--mount source=nginx-vol,destination=/usr/share/nginx/html,readonly \
nginx:latest
Use docker inspect nginxtest to verify that the readonly mount was created correctly. Look for
the Mounts section:
"Mounts": [
{
"Type": "volume",
"Name": "nginx-vol",
"Source": "/var/lib/docker/volumes/nginx-vol/_data",
"Destination": "/usr/share/nginx/html",
"Driver": "local",
"Mode": "",
"RW": false,
"Propagation": ""
}
],
Stop and remove the container, and remove the volume. Volume removal is a separate step.
There are several ways to achieve this when developing your applications. One is to add logic to
your application to store files on a cloud object storage system like Amazon S3. Another is to create
volumes with a driver that supports writing files to an external storage system like NFS or Amazon
S3.
Volume drivers allow you to abstract the underlying storage system from the application logic. For
example, if your services use a volume with an NFS driver, you can update the services to use a
different driver, as an example to store data in the cloud, without changing the application logic.
Initial set-up
This example assumes that you have two nodes, the first of which is a Docker host and can connect
to the second using SSH.
NFSV3
NFSV4
Backup a container
For example, create a new container named dbstore:
$ docker run -v /dbdata --name dbstore ubuntu /bin/bash
When the command completes and the container stops, we are left with a backup of
our dbdata volume.
Then un-tar the backup file in the new container`s data volume:
You can use the techniques above to automate backup, migration and restore testing using your
preferred tools.
Remove volumes
A Docker data volume persists after a container is deleted. There are two types of volumes to
consider: