Sub topic 13
Backing up and restoring a volume
This chapter assumes you can run containers with docker run, you know how named volumes work, and you can read a basic shell command with -v mounts. A named volume lives in Docker's storage area on the host. You don't normally open that area by hand, so you need a way to copy data in and out. The standard method is a short-lived helper container.
The idea
A helper container mounts two things at once: the volume you want to copy and a host directory. It runs tar to move data between them. Then it exits, and --rm deletes it.
This works the same on any host and needs nothing beyond the alpine image. It also doesn't depend on where Docker keeps volumes on disk.
Back up a volume
Run this from the directory where you want the archive to land:
docker run --rm \
-v pgdata:/data:ro \
-v "$(pwd)":/backup \
alpine tar czf /backup/pgdata.tgz -C /data .-v pgdata:/data:romounts the volume read-only, so the backup can't change it.-v "$(pwd)":/backupmounts the current host directory. This is a bind mount, because the source is a path.tar czfcreates a gzip-compressed archive.-C /data .changes into/datafirst. The archive then holds paths like./PG_VERSIONinstead ofdata/PG_VERSION, so you can restore it into any directory.
After the command finishes, pgdata.tgz is in your current directory.
Restore into a volume
Restoring reverses the direction. The volume is mounted read-write and the archive is read from the host:
docker run --rm \
-v pgdata:/data \
-v "$(pwd)":/backup \
alpine tar xzf /backup/pgdata.tgz -C /dataIf the volume doesn't exist yet, Docker creates it when this container starts. That makes the same command useful for moving data to a new machine. Copy the .tgz file there and run the restore.
Get a consistent copy
Copying files while a program is writing to them can produce a broken backup. A database is the usual case. For a simple, safe backup:
- Stop the container that uses the volume, for example
docker stop db. - Run the backup command.
- Start the container again with
docker start db.
If you can't afford downtime, use the database's own tool instead. For PostgreSQL, run pg_dump inside the container and write a logical dump. Volume-level tar is best for data that is idle, or for stores that can tolerate a file copy.
Reading data from another container's volumes
The --volumes-from flag gives a helper container the same mounts as an existing container. It saves you from naming the volume:
docker run --rm --volumes-from db \
-v "$(pwd)":/backup \
alpine tar czf /backup/db-data.tgz -C /var/lib/postgresql/data .Here the path after -C must be the mount point used inside db. The db container can be stopped or running, and it must still exist. The helper only reuses its volume mounts and does not start it.
Check your archive
Before you rely on a backup, list its contents without extracting it:
docker run --rm -v "$(pwd)":/backup alpine tar tzf /backup/pgdata.tgzYou should see the files you expect. Better still, restore into a scratch volume and start a test container from it.
Exercise
You run docker run --rm -v pgdata:/data:ro -v "$(pwd)":/backup alpine tar czf /backup/pgdata.tgz -C /data . and later restore it into a volume that already holds newer files. What happens to the newer files that are not in the archive, and how do you avoid it?
Show answer
They stay in the volume, because extraction only adds or overwrites files from the archive. To get an exact copy of the backup, restore into a new, empty volume.
Exercise
Why does the backup command mount the volume with :ro, and why should you stop a database container before backing up its volume?
Show answer
:ro stops the helper container from changing the data it is copying. The database may be writing files while tar reads them, which can produce an inconsistent archive. Stopping the container first gives a clean copy.