How to Backup Docker Volumes: Simple Commands and Examples
To backup a Docker volume, use the
docker run command with a temporary container that mounts the volume and copies its data to a local folder or tar archive. For example, run docker run --rm -v volume_name:/volume -v $(pwd):/backup busybox tar czf /backup/backup.tar.gz -C /volume . to create a compressed backup.Syntax
The basic syntax to backup a Docker volume involves running a temporary container that mounts the volume and a local directory, then uses a command like tar to archive the volume's contents.
docker run --rm: Runs a temporary container that is removed after the command finishes.-v volume_name:/volume: Mounts the Docker volume inside the container at/volume.-v $(pwd):/backup: Mounts the current local directory to/backupinside the container to save the backup file.busybox: A small Linux image used to run the backup command.tar czf /backup/backup.tar.gz -C /volume .: Creates a compressed archive of the volume contents and saves it to the mounted backup folder.
bash
docker run --rm -v volume_name:/volume -v $(pwd):/backup busybox tar czf /backup/backup.tar.gz -C /volume .
Example
This example shows how to backup a Docker volume named mydata to a compressed file mydata-backup.tar.gz in your current directory.
bash
docker run --rm -v mydata:/volume -v $(pwd):/backup busybox tar czf /backup/mydata-backup.tar.gz -C /volume .
Output
The command runs silently and creates the file mydata-backup.tar.gz in the current directory.
Common Pitfalls
Common mistakes when backing up Docker volumes include:
- Not using
--rmwhich leaves temporary containers running. - Forgetting to mount the local directory to save the backup file.
- Using the wrong volume name or path inside the container.
- Not having permissions to write to the local backup folder.
Always verify the volume name with docker volume ls before backing up.
bash
docker run -v mydata:/volume busybox tar czf /backup/mydata-backup.tar.gz -C /volume . # This will fail because /backup is not mounted, so the backup file cannot be saved. # Correct way: docker run --rm -v mydata:/volume -v $(pwd):/backup busybox tar czf /backup/mydata-backup.tar.gz -C /volume .
Quick Reference
| Command | Description |
|---|---|
| docker volume ls | List all Docker volumes to find the volume name. |
| docker run --rm -v volume_name:/volume -v $(pwd):/backup busybox tar czf /backup/backup.tar.gz -C /volume . | Backup the volume to a compressed tar.gz file in the current directory. |
| docker run --rm -v volume_name:/volume -v $(pwd):/backup busybox tar xzf /backup/backup.tar.gz -C /volume | Restore the volume from a backup archive. |
Key Takeaways
Use a temporary container with volume mounts to backup Docker volumes safely.
Always mount a local directory to save the backup file outside the container.
Verify volume names with 'docker volume ls' before backing up.
Use 'tar' inside the container to create compressed backups of volume data.
Include '--rm' to automatically remove the temporary container after backup.