✦ For everyone, free.

Practical knowledge for real and everyday life

Home

Named Volume Backup

Named Volume Backup ensures data persistence and recoverability in Docker by securely copying container data to external storage

Named volume backup is the process of extracting a named volume's data into a portable archive, typically performed using a temporary container that mounts both the volume and a host location, providing a way to create durable backups independent of the volume's own ongoing existence.

The Standard Backup Pattern

A short-lived container mounts the volume to be backed up alongside a host directory, then archives the volume's contents into that host directory.

docker run --rm \
  -v app-data:/data:ro \
  -v $(pwd)/backups:/backup \
  alpine tar czf /backup/app-data-$(date +%Y%m%d).tar.gz -C /data .

This creates a timestamped archive of the volume's entire contents, written to a host directory where it can be retained, copied elsewhere, or otherwise managed independently of the volume itself.

Mounting the Volume Read-Only During Backup

Mounting the volume as read-only (:ro) during the backup process prevents the backup operation itself from accidentally modifying the data being backed up.

docker run --rm -v app-data:/data:ro -v $(pwd)/backups:/backup alpine tar czf /backup/backup.tar.gz -C /data .

Restoring From a Backup

The reverse operation extracts a previously created archive back into a volume, useful for restoring data after an incident or migrating data to a new environment.

docker run --rm \
  -v app-data-restored:/data \
  -v $(pwd)/backups:/backup \
  alpine tar xzf /backup/app-data-20260601.tar.gz -C /data

Automating Regular Backups

This same pattern can be scheduled to run periodically, ensuring regular, automated backups of important volume data without requiring manual intervention each time.

0 2 * * * docker run --rm -v app-data:/data:ro -v /backups:/backup alpine tar czf /backup/app-data-$(date +%Y%m%d).tar.gz -C /data .

Why Named Volume Backup Matters

Since a volume's persistence guards against container removal but not against host failure, disk corruption, or accidental volume deletion, establishing a reliable backup process is an essential complement to volume-based persistence for any data that genuinely cannot afford to be lost.