How to backup and restore Docker volumes?

· Category: Docker

Short answer

Back up Docker volumes by mounting them into a temporary container and creating an archive. Restore by extracting the archive into a new volume.

Steps

  1. Stop the container using the volume.
  2. Run a temporary container to archive the volume.
  3. Copy the archive to safe storage.
  4. Restore by creating a new volume and extracting the archive.

Example

# Backup
docker run --rm -v mydata:/data -v $(pwd):/backup ubuntu   tar czf /backup/backup.tar.gz -C /data .

# Restore
docker volume create mydata_new
docker run --rm -v mydata_new:/data -v $(pwd):/backup ubuntu   tar xzf /backup/backup.tar.gz -C /data

Tips

  • Automate backups with cron jobs or scheduled CI pipelines.
  • Test restores periodically to ensure backup integrity.
  • Use volume plugins that support snapshotting for large datasets.

Common issues

  • Backing up a volume while the container is running can corrupt data.
  • Large volumes require significant disk space for archives.
  • Permission differences between backup and restore environments can cause access issues.