Sub topic 14
Volume drivers and remote storage
This chapter assumes you can create, inspect, and remove named volumes, and that you have read the Compose chapter. Every volume you have made so far used the default local driver, which stores data on the Docker host. Volume drivers let a volume live somewhere else.
What a volume driver does
A volume driver is the component that creates, mounts, and removes a volume. Docker asks the driver for a mount point and then attaches that path to the container. The container sees an ordinary directory either way.
You can check which driver an existing volume uses:
docker volume create pgdata
docker volume inspect pgdata --format '{{ .Driver }}'
# localThe driver is fixed when the volume is created. You cannot change it afterward. To move data to a different driver, create a new volume and copy the data over, for example with the temporary-container method from the backup chapter.
The local driver with options
The local driver does more than use a host directory. It accepts mount options that follow the Linux mount command. This is the simplest way to use network storage, because it needs no extra software on the host beyond the NFS client.
docker volume create \
--driver local \
--opt type=nfs \
--opt o=addr=192.168.1.10,rw,nfsvers=4 \
--opt device=:/exports/data \
nfsdata
docker run --rm -v nfsdata:/data alpine ls /dataThe type option is the filesystem type, o holds the mount options, and device is the remote path. Docker does not mount the share when you create the volume. It mounts it the first time a container uses the volume. A wrong address therefore shows up as an error at docker run, not at docker volume create.
Third-party plugin drivers
For storage the local driver cannot reach, such as a cloud block device or a distributed filesystem, you install a volume plugin. A plugin is a program that runs alongside the Docker daemon and implements the volume API.
docker plugin ls
docker plugin install <plugin-name>
docker volume create --driver <plugin-name> --opt size=10 appdataOption names such as size belong to the plugin, not to Docker. Read the plugin's documentation for the options it accepts. docker plugin ls shows what is installed and whether each plugin is enabled.
Drivers in Compose
Compose passes the same settings through the top-level volumes key:
services:
app:
image: alpine
command: ls /data
volumes:
- nfsdata:/data
volumes:
nfsdata:
driver: local
driver_opts:
type: nfs
o: addr=192.168.1.10,rw,nfsvers=4
device: ":/exports/data"Quote the device value. A leading colon is otherwise easy to misread in YAML.
Common errors and trade-offs
- Network storage adds latency and a failure point. If the NFS server is unreachable, containers using the volume fail to start or hang on file access.
- Databases need care. Many databases misbehave on network filesystems because of locking and fsync semantics. Check the vendor's guidance before putting database files on NFS.
- Ownership still applies. NFS maps user IDs from the server, so permission problems from the earlier chapter can appear in a different form.
- Options are not validated at creation. A typo in
deviceoroonly fails when a container first mounts the volume. - Removing a volume may not delete remote data. What
docker volume rmdoes depends on the driver. With the NFS example above, the export on the server is left alone.
Exercise
You created a volume named shared with default settings, then realized it should be backed by NFS. Why can't you just run docker volume create --driver local --opt type=nfs ... shared, and what do you do instead?
Show answer
The name shared already exists, and a volume's driver and options are fixed at creation. Create a new volume with the NFS options under a new name, copy the data into it with a temporary container (for example with tar or cp), point your containers at the new volume, and then remove the old one with docker volume rm shared.