Latest
Docker 1 min read

Back Up and Restore Files in a Docker Volume

Back Up and Restore Files in a Docker Volume
Docker · September 30, 2026

Docker lab/version note: use supported Linux-container tools and patched images. Check version compatibility before running the examples. Docker, Swarm and platform-specific deployment steps were not tested live.

What this guide covers

A Docker volume keeps files outside a container’s writable layer. This guide copies test files into an archive and restores them into a separate volume, then checks contents and ownership.

Before you start

Use a disposable Docker environment. This is a file-backup exercise, not a live database-backup procedure. Databases need supported backup tools or a documented stop procedure so the backup is consistent. Keep real backups encrypted and off the original host.

Step 1: Create test data in new volumes

Run:

set -eu
for volume in reader-backup-source reader-backup-restore; do
  if docker volume inspect "$volume" >/dev/null 2>&1; then
    printf 'Stop: lab volume already exists: %s\n' "$volume" >&2
    exit 1
  fi
done
docker volume create reader-backup-source
docker volume create reader-backup-restore
docker run --rm -v reader-backup-source:/data alpine:3.23 \
  sh -ec 'mkdir -p /data/uploads; printf "restore-test\n" > /data/uploads/probe.txt; chown -R 1000:1000 /data/uploads; chmod 0750 /data/uploads; chmod 0640 /data/uploads/probe.txt'

The commands refuse to reuse existing volume names. The helper runs as root only inside these dedicated test volumes to set numeric file ownership.

Step 2: Create the archive

Keep the source unchanged while backing up. Run:

mkdir -p reader-backups
chmod 700 reader-backups
BACKUP_DIR=$(cd reader-backups && pwd)
export BACKUP_DIR
docker run --rm \
  -v reader-backup-source:/source:ro \
  --mount "type=bind,src=$BACKUP_DIR,dst=/backup" \
  alpine:3.23 sh -ec 'umask 077; tar -czf /backup/reader-files.tgz -C /source .'
test -s "$BACKUP_DIR/reader-files.tgz"
docker run --rm \
  --mount "type=bind,src=$BACKUP_DIR,dst=/backup,readonly" \
  alpine:3.23 tar -tzf /backup/reader-files.tgz

The source is mounted read-only. A successful archive command means files were copied; it does not yet prove restoration works.

Step 3: Restore into the empty destination

In the same terminal, run:

: "${BACKUP_DIR:?Use the protected absolute backup directory}"
docker run --rm \
  --mount "type=bind,src=$BACKUP_DIR,dst=/backup,readonly" \
  -v reader-backup-restore:/restore \
  alpine:3.23 sh -ec 'tar -xzf /backup/reader-files.tgz -C /restore'
docker run --rm --user 1000:1000 \
  -v reader-backup-restore:/data:ro \
  alpine:3.23 sh -ec 'test "$(cat /data/uploads/probe.txt)" = "restore-test"; test "$(stat -c %u:%g /data/uploads/probe.txt)" = "1000:1000"'

The restore must use the separate empty test volume. Check file content and numeric ownership before trying it with a real application.

Step 4: Plan real recovery

Identify all application files and database records that must agree at recovery time. Test restoration with the matching application version. A volume with the same name on another Docker host is not automatically the same data.

Check the result

The restored test file and ownership should match the source. Remove only the two named test volumes when finished and no longer needed. Do not delete real backups as part of test-container cleanup.

References

PB
Written by

Parul Bedi

Notes and guides by Parul Bedi.

Up next

Build Docker Images for Apple Silicon and Intel Machines

Docker October 2, 2026 1 min read
Open post

Conversation

0 comments

No comments yet. Start the conversation below.

Leave a comment

Replying to a comment.
Be kind. Markdown is not supported in comments.