$ run as a normal user# run as root (or with sudo)
The quick answer
docker run --rm -v shop_uploads:/data:ro -v "$PWD":/backup alpine:3.22 tar -czf /backup/uploads-$(date +%F).tar.gz -C /data .
docker volume create shop_uploads_restore
docker run --rm -v shop_uploads_restore:/data -v "$PWD":/backup alpine:3.22 tar -xzf /backup/uploads-2026-11-19.tar.gz -C /dataA small Alpine container mounts the volume (read-only for the backup) and your current folder, and runs tar.
Step by step
- Find the volumes and which container uses them:
docker volume ls
docker ps -a --filter volume=shop_uploads --format '{{.Names}}'
docker volume inspect shop_uploads --format '{{.Mountpoint}}'- Files (uploads, configs) — the quick answer works while the app runs (files written during the backup may be missing; for a perfect copy stop the app briefly).
- Databases — never copy the files of a running DB. Make a dump with the database's own tool, from the container:
docker compose exec -T db pg_dump -U shop -Fc shop > shop-2026-11-19.dump
docker compose exec -T mysql sh -c 'mysqldump --single-transaction -uroot -p"$MYSQL_ROOT_PASSWORD" shop' | gzip > shop-2026-11-19.sql.gz(Or stop the DB container, then tar its volume.)
- Restore files into a new or emptied volume (quick answer), then point the service to it or copy back. Check ownership: the app's user (for example UID 101 for nginx) must own the files —
tarkeeps the numeric owners. - Keep backups away from the host — copy them to object storage with versioning, or back up straight with restic:
docker run --rm -v shop_uploads:/data:ro -e RESTIC_REPOSITORY -e RESTIC_PASSWORD -e AWS_ACCESS_KEY_ID -e AWS_SECRET_ACCESS_KEY restic/restic:0.18.0 backup /data --tag uploads- Automate — a nightly timer/cron with the backup command, retention (keep 14), and a monthly test restore into a new volume.
Check that it worked
Common problems
| Problem | Cause | Fix |
|---|---|---|
| Database corrupt after restoring files | Files copied while the DB was running. | Use dumps, or stop the DB before the file backup. |
Permission denied in the app after restore | Owner/UID differs. | chown -R UID:GID inside a helper container; keep numeric owners. |
| Backup archive is empty | Wrong volume name (Compose prefixes PROJECT_). | docker volume ls; use the full name. |
Data gone after docker compose down -v | -v deletes named volumes. | Never use -v on real data; restore from backup. |
| Backups only on the same disk | Host loss = backup loss. | Copy off-host (S3 with versioning, restic). |
| SELinux denies the helper container | Bind mount without label. | Add :Z to the bind mount on SELinux hosts. |
Go further
- Command pages: docker, tar.
- Next how-tos: Back up and restore PostgreSQL, Back up a server with restic, Run an app with Docker Compose, Give a Pod persistent storage.
- Learn it: Docker — Volumes, Docker — Back up volumes. Full guide: restic.
- Practise: Lab: prove the backup restores.
1Why should you not tar the volume of a running PostgreSQL container?
The files change during the copy and the restore can be corrupt; use pg_dump or stop the DB first.
2How do you back up a named volume without installing tools on the host?
Run a throw-away container that mounts the volume and a host folder, and run tar inside it.