Backing up and migrating volume data
Back up a volume with a throwaway tar container, restore it, stream it to another host over SSH, and know when a database dump beats copying files.
Backing up and migrating volume data
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- Do not copy files out of /var/lib/docker/volumes directly. Mount the volume into a short-lived helper container and archive it from there.
- docker run --rm -v vol:/data:ro -v "$PWD":/backup debian tar -C /data -czf /backup/vol.tgz . is a complete, repeatable volume backup.
- Restore by creating a new volume and extracting into it with the same kind of helper. --numeric-owner keeps owners as numbers so a different image's user names cannot remap them.
- Piping tar through ssh moves a volume to another host without an intermediate file.
- Copying a live database's files can capture a half-written state. Stop the container first, or use the database's own dump or backup tool.
- A backup you have never restored is a hope, not a backup. Test restores into a scratch volume.
Volumes are not backups
A named volume survives docker rm. It does not survive a failed disk, a deleted VM, docker compose down -v typed in the wrong terminal, or docker volume prune --all. Moving to a new server also means moving its volumes. Both jobs need the same thing: getting a volume’s contents out as a file and back in again.
Backup strategy in general, recovery point and recovery time objectives, is in the devops backups lesson. This one is the Docker mechanics.
Back up with a helper container
A volume’s files sit under /var/lib/docker/volumes/<name>/_data on the host, and on Docker Desktop that is inside a VM you cannot easily reach. The docs call touching that directory directly unsupported. The standard approach is a throwaway container that mounts the volume and your current directory and runs tar:
docker run --rm \
-v pgdata:/data:ro \
-v "$PWD":/backup \
debian:trixie-slim \
tar -C /data --numeric-owner -czf /backup/pgdata-2026-11-28.tgz .
Read it as: mount the volume read-only at /data, mount the current directory at /backup, and archive the contents of /data into it. --rm removes the helper when it is done. The tar runs as root in the container, so it can read every file whatever its owner, and the archive records each file’s owner and mode.
The docs’ own example uses --volumes-from dbstore to borrow every volume of an existing container. That works, but naming the volume is clearer about what is being backed up.
Restore into a new volume
docker volume create pgdata-restored
docker run --rm \
-v pgdata-restored:/data \
-v "$PWD":/backup:ro \
debian:trixie-slim \
tar -C /data --numeric-owner -xzf /backup/pgdata-2026-11-28.tgz
Then point a container at pgdata-restored and check the application really starts. Restoring into a fresh volume rather than the original keeps the old copy until you are sure.
Why –numeric-owner
A tar header records each file’s owner twice, as a number and as a name. I built a small archive in Python’s tarfile with files owned by www-data, UID and GID 33, and listed it with tar on this Mac:
$ tar -tvf site-data.tar
drwxr-xr-x 0 www-data www-data 0 Nov 23 05:30 ./uploads/
-rw-r--r-- 0 www-data www-data 7 Nov 23 05:30 ./uploads/logo.png
-rw-r--r-- 0 root root 16 Nov 23 05:30 ./config.php
$ tar --numeric-owner -tvf site-data.tar
drwxr-xr-x 0 33 33 0 Nov 23 05:30 ./uploads/
-rw-r--r-- 0 33 33 7 Nov 23 05:30 ./uploads/logo.png
-rw-r--r-- 0 0 0 16 Nov 23 05:30 ./config.php
When GNU tar extracts as root, it maps owners by name if that name exists in the extracting system’s /etc/passwd and /etc/group, and only falls back to the number otherwise. The helper container’s files are what count, not the files of the app that owns the data. Names and numbers disagree between images more often than you would think. In the Docker Hub base images I checked, the www-data group is GID 33 in debian:trixie-slim and GID 82 in alpine:3.22. Restore a Debian-built WordPress volume through a helper that knows www-data as 82 and the files quietly change group. --numeric-owner on both sides keeps the numbers exactly, which is what the ownership lesson showed the kernel cares about.
Moving a volume to another host
Pipe the archive straight over SSH instead of writing it to disk:
docker run --rm -v pgdata:/data:ro debian:trixie-slim \
tar -C /data --numeric-owner -czf - . \
| ssh new-host 'docker run --rm -i -v pgdata:/data debian:trixie-slim \
tar -C /data --numeric-owner -xzf -'
The left side writes the archive to stdout. The right side, on the new host, reads it from stdin. The -i matters, without it the remote container’s stdin is not connected. The volume on the new host is created by the -v on first use. For large volumes, rsync between two helper containers that mount the volume, or between the hosts’ volume directories with the daemon stopped, can resume an interrupted copy, which tar over a pipe cannot.
Live databases need their own tools
tar reads files one after another. A database writing while you copy can leave the archive with a data file from one moment and its write-ahead log from another, which may not start, or may start with silent damage. Two safe options:
Stop the writer. docker stop pg, back up the volume, docker start pg. Simple, with a short outage.
Use the database’s own backup. It produces a consistent copy while the database runs:
docker exec pg pg_dump -U postgres -Fc appdb > appdb-2026-11-28.dump
docker exec -i pg pg_restore -U postgres -d appdb --clean < appdb-2026-11-28.dump
A logical dump also survives major-version upgrades, which a raw copy of the files does not. That is the Postgres 18 path change again: data files are tied to a major version, a dump is not. MySQL has mysqldump, MongoDB mongodump, Redis a BGSAVE snapshot.
Make it routine
Put the backup command in a script with dated file names, run it on a systemd timer or from CI, ship the archives off the host, and once a month restore one into a scratch volume and start the app against it. The restore test is the part that tells you whether the backup works.
About the video
The embed is Christian Lempa’s “Migrate Docker Volumes from one Host to another // backup and restore”, September 2021, 9 minutes. Its chapters cover how Docker stores container data, volumes compared with bind mounts, some example volumes and binds, accessing files inside named volumes, and backing up, restoring and migrating data with a helper container. It predates nothing important here, since the approach has not changed, but it does not discuss database consistency or numeric owners. English was judged from the channel and the English caption track. WittCode’s video in deeper is a shorter walk through the same tar commands.
What was run: the tar listings are real, from an archive built with Python’s tarfile and listed with the system tar on this Mac. The www-data GIDs were read from the /etc/group files in the debian:trixie-slim and alpine:3.22 layers, fetched from Docker Hub’s registry API. The Docker commands were written from the volumes page and not run, since there is no Docker here. GNU tar’s name-first ownership mapping is from its manual, and the pg_dump advice from the PostgreSQL backup documentation, checked 2026-11-28.
Where this fits
That ends storage. Next, how containers talk to each other and to the world, starting with the network every container joins by default: the default bridge network.
Check yourself
Why mount the volume read-only in the backup container?
The helper only needs to read, and :ro guarantees a typo in the tar command cannot modify or delete the data it is backing up.
You back up a WordPress volume whose uploads are owned by www-data, UID and GID 33 in Debian, and restore it with GNU tar as root in a helper image where the www-data group is GID 82. What can go wrong, and how do you prevent it?
GNU tar restores owners by the names recorded in the archive when those names exist locally, so the files could get GID 82 and the web server could lose access. Extract with --numeric-owner to keep 33.
Is tarring a running Postgres container's data volume a valid backup?
Not reliably. Files are copied at different moments while the database writes, so the copy can be inconsistent. Stop the container first, or use pg_dump or pg_basebackup, which produce consistent backups while it runs.
How do you move a volume to a new server without writing an archive to disk?
Stream it: tar the volume in a helper container on the old host to stdout, pipe it through ssh, and extract it in a helper container on the new host that mounts a new volume of the same name.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.