To take a docker volume backup, run a throwaway alpine (or busybox) container that mounts the named volume read-only and tars its contents to a bind-mounted host directory: docker run --rm -v myvol:/data:ro -v $(pwd):/backup alpine tar czf /backup/myvol.tar.gz -C /data .. To restore, create a fresh empty volume and run the inverse — untar the archive into the new volume. That covers file data cleanly. For a live database, the raw copy can be torn and unusable, so you either stop the container first or use the engine’s own dump tool (mysqldump, pg_dump) instead of copying files. This guide walks through both paths and how to prove the restore actually worked.
What you’ll accomplish
- Back up any Docker named volume to a single portable
.tar.gzarchive with no extra tooling. - Restore that archive into a fresh volume and re-point your app or Compose service at it.
- Verify a restore by listing files, checksumming a sample, and starting the app against the restored data.
- Recognize when a raw file copy is unsafe (live databases) and use a consistent dump instead.
- Know where this pattern stops and a real backup tool should take over.
Prerequisites
- Docker Engine 24 or newer (the commands are unchanged through Docker 28). Check with
docker --version. - A named volume that is actually in use by a container — this guide backs up volume data, not bind mounts.
- Free disk space on the host for the archive. A
.tar.gzis compressed, but you still need room for it plus, during restore, the expanded copy inside the new volume. - Knowing whether the volume holds a live database. If it does, read the DB-safe step before you copy anything — a raw tar of an open database file can capture a half-written state.
How it works
Docker named volumes live on the host under /var/lib/docker/volumes/<name>/_data. You could copy that directory directly, but it is owned by root, its exact path is an implementation detail, and it does not exist at all on Docker Desktop (where the engine runs inside a VM). The portable approach is the sidecar pattern: start a small throwaway container, mount the target volume into it, and let that container read or write the data through the mount instead of touching the host path.
For a backup, the sidecar mounts the volume read-only at /data and a host directory at /backup, then runs tar to stream /data into a compressed archive in /backup. The container exits and is removed (--rm); the archive is left behind on the host. Restore is the exact inverse: a fresh, empty volume is mounted read-write at /data, the host directory holding the archive is mounted at /backup, and tar extracts the archive back into /data.
The key detail is the -C /data . in the tar command. -C /data changes into the data directory before archiving, and . archives its contents rather than the directory itself. That makes the archive contents relative, so they extract cleanly into any target /data on restore. Skip it and you get either an empty archive or one with an absolute path baked in that will not restore where you expect.
Step-by-step
1. Find the volume you want to back up. List volumes and confirm the exact name.
docker volume ls
Expected output — your volume appears in the list:
DRIVER VOLUME NAME
local myvol
Inspect it if you want to confirm the mountpoint and which container uses it:
docker volume inspect myvol
The output includes the on-host Mountpoint, which confirms the volume exists and is a local driver volume:
"Mountpoint": "/var/lib/docker/volumes/myvol/_data",
"Name": "myvol",
"Scope": "local"
2. Back up the volume to a compressed archive. Run the sidecar. The :ro makes the mount read-only so the backup cannot alter live data, and $(pwd) bind-mounts your current directory as /backup.
docker run --rm -v myvol:/data:ro -v $(pwd):/backup alpine tar czf /backup/myvol.tar.gz -C /data .
There is no output on success — tar is quiet unless you pass -v, and the container exits 0. The success signal is the file appearing in your current directory (next step).
3. Verify the tarball exists and is not empty. A silent success can still be an empty archive if the path was wrong, so always check.
ls -lh myvol.tar.gz && tar tzf myvol.tar.gz | head
Expected output — a non-zero size and a listing of real files inside:
-rw-r--r-- 1 you staff 4.2M Jul 20 09:14 myvol.tar.gz
./
./config.json
./data/
./data/app.db
If the size is a few hundred bytes and the listing shows only ./, the volume was empty or the mount path was wrong — recheck step 2 before relying on this backup.
4. Create a fresh, empty volume to restore into. Restore into a new volume rather than overwriting the original, so you can compare and roll back.
docker volume create myvol_new
Expected output — Docker echoes the new name:
myvol_new
5. Restore the archive into the new volume. Mount the new volume read-write and the directory holding the archive, then extract.
docker run --rm -v myvol_new:/data -v $(pwd):/backup alpine tar xzf /backup/myvol.tar.gz -C /data
Again there is no output on success and the container exits 0. Confirm the data landed:
docker run --rm -v myvol_new:/data alpine ls -la /data
Expected output — the files from the original volume are now present:
drwxr-xr-x 3 root root 4096 Jul 20 09:20 .
-rw-r--r-- 1 root root 218 Jul 20 09:14 config.json
drwxr-xr-x 2 root root 4096 Jul 20 09:14 data
6. Re-point the app at the restored volume. In a plain docker run, swap the volume name (-v myvol_new:/var/lib/app). In Compose, update the service’s volume reference. If your Compose file declares the volume, point it at the external restored one:
services:
app:
image: myapp:latest
volumes:
- myvol_new:/var/lib/app
volumes:
myvol_new:
external: true
Then recreate the service so it mounts the new volume:
docker compose up -d
7. For a live database, do not raw-copy — quiesce or dump. A tar of an open database captures files mid-write and can restore to a corrupt state. Take one of two consistent paths. Either stop the container first so nothing is writing, then back up as in step 2:
docker stop myapp-db
docker run --rm -v pgdata:/data:ro -v $(pwd):/backup alpine tar czf /backup/pgdata.tar.gz -C /data .
docker start myapp-db
Or, better, leave it running and use the database’s own dump tool, which produces a point-in-time consistent export:
docker exec myapp-db pg_dump -U postgres appdb > appdb.sql
# MySQL / MariaDB equivalent:
docker exec myapp-db mysqldump -u root -p appdb > appdb.sql
You restore a dump by feeding it back with psql or mysql, not by extracting a tar — the file is SQL, not a volume image.
Validate it works
Never trust a backup you have not restored and checked. First list the restored tree and compare it to the source:
docker run --rm -v myvol_new:/data alpine ls -R /data | head
Then checksum a known file in both the original and the restored volume — the hashes must match:
docker run --rm -v myvol:/data:ro alpine md5sum /data/config.json
docker run --rm -v myvol_new:/data:ro alpine md5sum /data/config.json
Expected output — identical checksums (only the path differs):
5eb63bbbe01eeed093cb22bb8f5acdc3 /data/config.json
5eb63bbbe01eeed093cb22bb8f5acdc3 /data/config.json
Finally, start the real application against myvol_new and confirm it reads the data — the app boots, the config loads, and records are present. For a database, restore the dump into a fresh instance and run a SELECT count(*) against a known table to confirm the row counts match the source.
Troubleshooting
The archive is empty or tiny. Almost always a missing -C /data .. Without -C /data, tar archives nothing useful; without the trailing ., it has no files to add. Re-run step 2 exactly, then verify with tar tzf myvol.tar.gz | head before trusting it.
Permissions or ownership look wrong after restore. tar with the default flags preserves ownership by UID/GID, but if the source and target run different users, files can end up owned by an unexpected UID. Extracting as root (the default in the alpine sidecar) keeps numeric ownership; if your app runs as a specific non-root UID, confirm that UID still matches after restore, and re-chown inside a sidecar if needed.
Restore silently merges into a non-empty volume. Extracting into a volume that already has data overlays the archive on top of what is there, leaving a mix of old and new files. Always restore into a freshly created volume (step 4). If you must reuse a volume, clear it first inside a sidecar (alpine sh -c "rm -rf /data/*") — and be sure that is what you intend.
A live-database backup restores as corrupt. You raw-copied an open database and captured a torn write. Redo it with the container stopped, or use pg_dump/mysqldump (step 7). There is no way to un-tear a bad copy after the fact.
SQLite backup is inconsistent. A busy SQLite database in WAL mode keeps recent writes in a separate -wal file. A tar that catches the main .db but misses an in-flight -wal can restore to a stale or broken state. Stop the writer first, or use sqlite3 app.db ".backup '/backup/app.db'" to get a consistent snapshot before archiving.
When this is the wrong approach
The tar sidecar is right for ordinary file data in a named volume — uploads, generated assets, config, a stopped database’s files. It is the wrong tool in three cases. First, live databases: use the engine’s native dump (pg_dump, mysqldump, mongodump) for a consistent, point-in-time export rather than copying files under an active writer. Second, large or many volumes on a schedule: a shell loop over docker run has no retention, deduplication, encryption, or offsite copy — reach for a real backup tool like restic or, in Kubernetes, Velero, which handle incremental snapshots and remote storage. Third, bind mounts are not volumes: a bind mount is just a host directory, so back it up like any other host path with your normal filesystem backup, not this pattern.
For related container hygiene, see Docker healthchecks to confirm a restored service is actually ready, and Docker multi-stage builds for keeping the images around your data lean. The authoritative reference is the Docker volumes documentation.
If this is the kind of work you’d rather never own again, it’s the daily job of Managed Cloud Ops — book an infrastructure review.