Sub topic 3
Persisting data with named volumes
This chapter assumes you can run docker run, docker ps, and docker rm. You should also know that containers start from images and that you can use a Linux shell. The previous chapters showed that data in a container's writable layer disappears with the container, and that Docker offers three storage types. This chapter covers the first one, the named volume.
What a named volume is
A named volume is a directory that Docker creates and manages on the host. You refer to it by name, and you mount it at a path inside a container. The container reads and writes that path as if it were an ordinary directory. The data lives in the volume, not in the container's writable layer, so removing the container leaves the data in place.
You do not need to know where Docker keeps the directory. On Linux with the default local driver it is under /var/lib/docker/volumes/, but you should treat that location as Docker's business.
Persisting a database
The most common use is a database. PostgreSQL stores its files in /var/lib/postgresql/data, so that is the path to mount the volume on.
docker volume create pgdata
docker run -d --name db -e POSTGRES_PASSWORD=example \
-v pgdata:/var/lib/postgresql/data postgres:16
# Remove the container; the volume and its data remain.
docker rm -f db
docker run -d --name db2 -e POSTGRES_PASSWORD=example \
-v pgdata:/var/lib/postgresql/data postgres:16The second container, db2, starts with the database files that db wrote. The -v pgdata:/var/lib/postgresql/data argument has two parts separated by a colon. The left side is the volume name. The right side is the path inside the container.
Running docker volume create first is optional. If the named volume does not exist when you run the container, Docker creates it for you.
Common errors
Most problems with named volumes come from a few mistakes.
- Wrong mount path. If you mount the volume somewhere the application does not write, the data still goes to the writable layer and is lost. Check the image's documentation for the data directory.
- Typing a path instead of a name. In
-v, a value that starts with/or./is a bind mount, not a volume.-v pgdata:/datais a volume.-v /pgdata:/datais a host directory. - Misspelled volume name. Docker does not warn you. It silently creates a new, empty volume, and your database appears to have lost its data. Run
docker volume lsto see which volumes exist. - Two databases on one volume. Never mount the same database volume into two running database containers. Both would write the same files and corrupt them.
Checking that it worked
To confirm that the data survived, write something, remove the container, and read it back. Here the volume is notes, mounted in a throwaway Alpine container.
docker run --rm -v notes:/data alpine sh -c 'echo kept > /data/file.txt'
docker run --rm -v notes:/data alpine cat /data/file.txtThe second command prints kept. Both containers were removed by --rm, yet the file remains because it was stored in notes. Compare this with the first chapter's example, where the same kind of write to the container's own filesystem was lost.
Exercise
You run docker run -d --name app -v appdata:/var/lib/app myimage, remove the container, and start a new one with -v appdata:/var/lib/app. Your data is missing. Name two likely causes.
Show answer
The application may not store its data in /var/lib/app, so the data went to the container's writable layer instead of the volume. Or the volume name was misspelled in the second command, so Docker created a new empty volume. Running docker volume ls shows whether a second volume was created.
Exercise
What is the difference between -v pgdata:/data and -v ./pgdata:/data in the way Docker interprets the left side?
Show answer
pgdata has no leading slash or dot, so Docker treats it as a named volume. ./pgdata is a path, so Docker treats it as a bind mount of that host directory.