Geavanceerd
Zoeken inHow-to-gidsen
Zo zoek je
  • Typ de taak in je eigen woorden (poort vinden, firewall openen, schijf vol) of plak een foutmelding tussen aanhalingstekens.
  • Kies "Snel antwoord" om alleen de commando's te zien, of een gebied om in één vakgebied te blijven.
  • "twee woorden" vindt precies die zin.
  • Gewone woorden vinden namen, teksten en voorbeelden.
  • ↑ ↓ bewegen · Enter openen · Esc sluiten · / zoeken
Probeer:

How-to 146Containers · Docker

How to back up and restore a Docker volume

Named volumes hold the data that matters — databases, uploads, configs — and they live under /var/lib/docker/volumes, outside any image. docker compose down -v or a lost host deletes them. This how-to backs up a volume to a tar.gz with a throw-away container, restores it into a new volume, and explains when a database needs a dump instead of a file copy.

  • 5 Lessen
  • 7 Commando's

Dit deel is nog niet vertaald. Je ziet de Engelse tekst.

$ als gewone gebruiker# als root (of met sudo)

146.1

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 /data

A small Alpine container mounts the volume (read-only for the backup) and your current folder, and runs tar.

146.2

Step by step

  1. 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}}'
  1. 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).
  2. 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.)

  1. 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 — tar keeps the numeric owners.
  2. 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
  1. Automate — a nightly timer/cron with the backup command, retention (keep 14), and a monthly test restore into a new volume.

146.3

Check that it worked

tar -tzf uploads-2026-11-19.tar.gz | head
docker run --rm -v shop_uploads_restore:/data alpine:3.22 ls -la /data | head

The archive lists the files, and a restored volume mounted into a test container shows the data with the right owners.

146.4

Common problems

ProblemCauseFix
Database corrupt after restoring filesFiles copied while the DB was running.Use dumps, or stop the DB before the file backup.
Permission denied in the app after restoreOwner/UID differs.chown -R UID:GID inside a helper container; keep numeric owners.
Backup archive is emptyWrong 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 diskHost loss = backup loss.Copy off-host (S3 with versioning, restic).
SELinux denies the helper containerBind mount without label.Add :Z to the bind mount on SELinux hosts.

146.5

Go further

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.

Alle commando's in dit hoofdstuk