Bottom line: Copying a volume directory while a container writes produces a torn snapshot — half-updated SQLite files, mismatched blobs. Stop-then-copy is correct but causes downtime; a bind mount plus a filesystem snapshot is better. Backing up a live volume with cp is the common mistake.

Why cp is wrong

A database page is written mid-copy, so the backup contains a state that never existed. SQLite may then report corruption on restore, WAL and data file disagree, and nobody notices until the restore.

The three approaches

Method Consistent Downtime
docker stop + copy yes full copy duration
Snapshot filesystem + copy yes seconds
cp while running no none

Snapshot approach (btrfs)

1
2
3
4
5
docker run --rm -v vol:/v -v /snaps:/s alpine \
sh -c 'sleep 1'
sudo btrfs subvolume snapshot -r /var/lib/docker/volumes/vol/_data /snaps/vol-$(date +%s)
restic backup --host "$HOST" --tag dockervol /snaps/vol-*
sudo btrfs subvolume delete /snaps/vol-*

On ZFS, zfs snapshot. Ext4 without LVM has no cheap option — use stop-then-copy.

Stop-then-copy, done safely

1
2
3
4
docker stop --time 30 app
trap 'docker start app' EXIT
docker run --rm -v vol:/v -v "$PWD":/b alpine \
tar -C /v -czf /b/vol.tgz .

The trap guarantees the restart even if tar fails. Without it, one error leaves the service down until someone notices.

Application-level dumps

Where the app supports it, dump rather than copy files: pg_dump, sqlite3 .backup, mysqldump. These are consistent by construction and usually smaller. A PostgreSQL volume copy without a dump is only safe with a filesystem snapshot underneath.

Verify the restore

A backup that has never been restored is a hypothesis. Monthly, restore the newest archive into a scratch volume, start the container on it, and query a row count. Automate the query assertion so a broken archive fails the job.

Retention

Restic with --keep-daily 7 --keep-weekly 4 --keep-monthly 6, pruned by the same timer that backs up. Untested retention expires into nothing exactly when it is needed.

Operating notes

Snapshot before copying. cp on a live volume yields a state that never existed and restores as corruption rather than an error.

1
2
btrfs subvolume snapshot -r /var/lib/docker/volumes/vol/_data /snaps/vol-$(date +%s)
restic backup --tag dockervol /snaps/vol-*

Stop-then-copy fallback

1
2
docker stop --time 30 app; trap 'docker start app' EXIT
docker run --rm -v vol:/v -v "$PWD":/b alpine tar -C /v -czf /b/vol.tgz .

The trap is not optional — one tar error otherwise leaves the service down.

Prefer dumps

pg_dump, sqlite3 .backup, mysqldump: consistent by construction. A file copy of a database volume needs a snapshot underneath to be safe.

Restore drill

Monthly: restore the newest archive into a scratch volume, start the container, assert a row count. An unrestored backup is a hypothesis.

FAQ

Why does copying a Docker volume with cp produce a corrupt backup?

A database page can be written mid-copy, so the backup captures a state that never existed. On restore, SQLite may report corruption, and the WAL and data file can disagree. The failure often goes unnoticed until the restore is actually attempted.

How do I back up a Docker volume without downtime?

Use a bind mount plus a filesystem snapshot: snapshot the volume’s data directory, then copy from the read-only snapshot. On btrfs use btrfs subvolume snapshot -r; on ZFS use zfs snapshot. Downtime drops to seconds instead of the full copy duration.

What is the safest way to stop a container and copy its volume?

Stop the container with docker stop --time 30, then run a temporary container that tars the volume to a backup file. Set a trap 'docker start app' EXIT so the service restarts even if tar fails. Without the trap, one error leaves the service down.

Why should I use pg_dump or sqlite3 .backup instead of copying database files?

Application-level dumps are consistent by construction and usually smaller than file copies. A PostgreSQL volume copy without a dump is only safe with a filesystem snapshot underneath. Where the app supports a dump, prefer it over copying files.

How do I verify that a Docker volume backup actually restores?

Monthly, restore the newest archive into a scratch volume, start the container on it, and query a row count. Automate the query assertion so a broken archive fails the job. A backup that has never been restored is a hypothesis.