tar: ./pg_wal: file changed as we read it is the line tar printed, with exit code 1, while copying a running PostgreSQL volume on a test container for this post. The archive still landed on disk at 19 MB, looking like any other backup, and the cron job that made it would have logged a failure that most people never read. A backup of a Docker stack can look finished and still fail on restore night, and the reasons are mostly about which kind of storage holds the data and whether a database was writing while it was copied.
Named volumes, bind mounts, and where the bytes live
Two kinds of mount, two different backup jobs
Docker’s own documentation says a volume is “stored within a directory on the Docker host”, and its example inspect output shows that directory as /var/lib/docker/volumes/my-vol/_data. Volumes are managed by Docker; bind mounts, by contrast, depend on the directory structure and OS of the host machine, because they are just a host folder pushed into the container. The same page says volumes are easier to back up or migrate than bind mounts, which is true for moving them and slightly misleading for finding them: a bind mount sits in a folder the owner chose, while a named volume sits under a Docker-owned path that a host-level file backup may skip.
On the test container, a small Compose file declared one named volume for the database and one bind mount for dumps:
services:
db:
image: localtest/pg16:lab
volumes:
- dbdata:/var/lib/postgresql/data
- ./backups:/backups
volumes:
dbdata:After docker compose up -d from a directory called shop, Docker reported this:
$ docker volume ls
DRIVER VOLUME NAME
local shop_dbdata
$ docker volume inspect shop_dbdata --format '{{ .Mountpoint }}'
/var/lib/docker/volumes/shop_dbdata/_data
The volume is not called dbdata. Compose put the project name in front of it, and that prefix comes back later as one of the nastier ways to lose a database without deleting anything.
Volumes also outlive containers. Docker states that data is persisted even if the container using it is removed, and that an unused volume isn’t removed automatically. That durability is why people treat a volume as safe, and it is the same durability that lets an old, orphaned volume sit there for a year while everyone assumes it was backed up along with the rest.
A test note on the setup: this sandbox could not reach Docker Hub, so the PostgreSQL 16 image here was built locally from an Ubuntu minimal root filesystem with a small start script. It is not the official postgres image, but it mounts data at the same path that image uses for versions 17 and below.

Database dumps versus copying the volume
A tar of a live database is a photograph of a moving engine
PostgreSQL’s manual is blunt about file-level copies. The database server must be shut down in order to get a usable backup, and the same passage adds that half-way measures such as disallowing all connections will not work, partly because tar does not take an atomic snapshot of the file system. A volume tar taken while Postgres runs is exactly that half-way measure.
pg_dump works the other way around. Its documentation says it makes consistent backups even if the database is being used concurrently and does not block readers or writers. The custom format, -Fc, is compressed by default and is the input pg_restore expects.
The test database got one table, orders, with 200,000 rows, and a fingerprint of its contents:
$ docker compose exec -T db psql -U postgres -At \
-c "SELECT count(*), md5(string_agg(id||item||qty, ',' ORDER BY id)) FROM orders;"
200000|db0eb5eff70fcb42932d3ba7446a0a52
Then a dump, written through the bind mount so it lands in a plain host folder:
$ time docker compose exec -T db pg_dump -U postgres -Fc -f /backups/shop.dump postgres
real 0m0.435s
$ du -h /var/lib/docker/volumes/shop_dbdata/_data backups/shop.dump
87M /var/lib/docker/volumes/shop_dbdata/_data
940K backups/shop.dump
The volume held 87 MB on disk; the dump of the same data came to 940 KB. A raw data directory carries write-ahead log segments, free space inside table files and catalog overhead, while the dump carries only what is needed to rebuild the rows. On this test data the gap was close to a hundred to one, and that ratio will differ on real data with indexes and large text, so read it as one measurement, not a rule.
For a SQLite-backed app the logic is the same with different tools, and the x9m8 post on backing up a live SQLite database runs through .backup and VACUUM INTO with their own test output.
Running the hot copy anyway
The test that came back fine, which is the problem
To see what a careless nightly job produces, a 400,000-row insert was started and, 0.3 seconds later, a throwaway container tarred the live volume using the same --volumes-from pattern Docker’s documentation shows for backups:
$ docker run --rm --volumes-from shop-db-1 -v /lab/shop/backups:/out \
localtest/pg16:lab tar czf /out/hot-copy.tgz -C /var/lib/postgresql/data .
tar: ./pg_wal: file changed as we read it
tar exit: 1
When the insert finished, the live table held 600,000 rows. The hot copy was extracted into a fresh volume and PostgreSQL started on it:
LOG: database system was not properly shut down; automatic recovery in progress
LOG: redo starts at 0/14EFB70
LOG: redo done at 0/4FFFF98 ...
LOG: database system is ready to accept connections
$ docker exec hot psql -U postgres -At -c "SELECT count(*) FROM orders;"
200000
It worked. Postgres treated the copy like a server that had lost power, replayed its log, and came up with the 200,000 committed rows; the in-flight insert was correctly absent. Anyone running this test once would walk away believing hot volume copies are fine.
The manual’s warning is about the cases this single run did not hit. Tar reads files one after another over seconds or minutes, so on a busy database the table files and the log can come from different moments, and crash recovery assumes a consistent crash, not a collage. One lucky run proves the restore path for that run. It proves nothing about the copy taken during a vacuum, or during a 3 a.m. import, which is when the cron job fires.

A backup routine for a Compose stack
Dump first, then copy the files, then write down the recipe
The order matters more than the tools. A routine that held up on the test container:
- Dump each database from inside its container into a bind-mounted folder, as above. For Postgres,
pg_dumpcovers one database; the docs point to pg_dumpall for roles and tablespaces, which a single-database dump leaves behind. - For volumes that hold plain files (uploads, config, media), a tar through a temporary container is fine, and safest with the app stopped. With the database stopped, the same tar was clean:
$ docker compose stop db
$ docker run --rm -v shop_dbdata:/data:ro -v /lab/shop/backups:/out \
localtest/pg16:lab tar czf /out/cold-copy.tgz -C /data .
tar exit: 0
$ docker compose start db
$ du -h backups/cold-copy.tgz
32M backups/cold-copy.tgz
- Back up the Compose project itself:
compose.yaml, any.envfile, and the bind-mounted folders. A volume without the file that names and mounts it is a box of parts with no drawing. - Ship the result off the host. A dump sitting in
./backupson the same disk as the volume dies with that disk. RAID does not change that, for reasons laid out in the x9m8 post on why RAID is not a backup.
The cold copy costs a few seconds of downtime. On a home lab that is usually nothing; on anything with users it is a scheduling decision, and that is why the dump is the primary backup and the file copy is the fallback.
Where people get burned
Four ways to lose a stack without typing rm
docker compose down -v. By default, down removes containers and networks; Docker’s reference says anonymous volumes are not removed by default, while -v removes named volumes declared in the “volumes” section. On the test container:
$ docker compose down
Container shop-v2-db-1 Removed
$ docker volume ls --format '{{.Name}}' | grep shop
shop-v2_dbdata
shop_dbdata
$ docker compose down -v
Volume shop-v2_dbdata Removed
That single flag took the database volume with it, and Compose asked for no confirmation first.
Renaming or copying the project folder. Compose assigns the project name based on the name of the directory unless -p, COMPOSE_PROJECT_NAME or a top-level name: overrides it. The shop folder was copied to shop-v2 and brought up:
Volume shop-v2_dbdata Created
$ docker compose exec -T db psql -U postgres -At -c "SELECT count(*) FROM orders;"
ERROR: relation "orders" does not exist
Nothing was deleted. The stack simply started with a brand-new empty volume while the real data sat untouched in shop_dbdata, which feels exactly like data loss for the first ten minutes. Setting name: at the top of the Compose file pins it.
Mounting at the wrong path. The official Postgres image page warns that for versions 17 and below, a mount at /var/lib/postgresql instead of /var/lib/postgresql/data WILL NOT PERSIST database data when the container is re-created; the image’s own declared volume catches the writes as an anonymous volume. The same page says the PGDATA variable was changed to be version specific in PostgreSQL 18 and above, so a Compose file copied from an older guide can point at the wrong place after an upgrade.
Init scripts that silently do nothing. Scripts in /docker-entrypoint-initdb.d are only run if you start the container with a data directory that is empty. A restore plan built on “the init script will recreate the schema” fails quietly the moment an old volume is still attached.

The restore test is the backup
A dump nobody has restored is a rumor
The last step on the test container was the only one that proved anything. A new volume, a new container, the dump fed back in, and the same fingerprint query:
$ docker volume create restoretest
$ docker run -d --name restoretest -v restoretest:/var/lib/postgresql/data \
-v /lab/shop/backups:/backups localtest/pg16:lab
$ docker exec restoretest pg_restore -U postgres -d postgres /backups/shop.dump
pg_restore exit: 0
$ docker exec restoretest psql -U postgres -At \
-c "SELECT count(*), md5(string_agg(id||item||qty, ',' ORDER BY id)) FROM orders;"
200000|db0eb5eff70fcb42932d3ba7446a0a52
Same count, same hash, so the bytes made the round trip. A row count alone would not have caught a truncated text column. The hash would.
Restoring into a separate volume on a separate container also keeps the test from touching production, the way a plumber pressure-tests a new line before tying it into the main. If the stack runs other databases, each needs its own check, and a monitor that alerts when the nightly dump file stops growing is cheap insurance; the x9m8 Uptime Kuma post covers push monitors that fire when a scheduled job goes quiet. For the general shape of a self-hosted Compose service, the x9m8 Compose walkthrough has the starting file.
The strongest argument against all of this is time: dumps, cold copies, off-site sync and a monthly restore drill are real work for a home server running a recipe app. Some owners decide the data is replaceable and skip it, and for a cache or a media index that call is defensible. The trouble is that most people make that decision by default, not on purpose, and they find out which kind of data they had on the morning the disk clicks.
When did a backup on a home server last get restored somewhere other than the machine that made it?
Sources
- Docker Docs, Volumes: https://docs.docker.com/engine/storage/volumes/
- Docker Docs, docker compose down reference: https://docs.docker.com/reference/cli/docker/compose/down/
- Docker Docs, Specify a project name: https://docs.docker.com/compose/how-tos/project-name/
- PostgreSQL 17 Documentation, File System Level Backup: https://www.postgresql.org/docs/17/backup-file.html
- PostgreSQL 17 Documentation, pg_dump: https://www.postgresql.org/docs/17/app-pgdump.html
- Docker Hub, postgres official image: https://hub.docker.com/_/postgres
- x9m8, Back up a live SQLite database: https://x9m8.com/sqlite-backup-safely-while-running/
- x9m8, RAID is not a backup: https://x9m8.com/raid-is-not-a-backup-raid-levels/
- x9m8, Uptime Kuma: https://x9m8.com/uptime-kuma-self-hosted-monitoring/
- x9m8, Docker Compose self-hosting: https://x9m8.com/docker-compose-self-host-service/
