How to Backup and Restore Docker Volumes Between Machines
When working with Docker, persistent data is often stored in volumes. Unlike container filesystems, volumes survive restarts and recreations. But what if you need to move this data from one machine to another?
Here’s a quick and reliable way to back up a Docker volume on one computer and restore it on another.
I find this use case very often when I’m working on a new project that changes the database schema and I need to maintain existing code with old schema.
To keep both the new project and the existing code working, I create a new volume from my backup, attach it to my docker-compose database service and work on the new project. When I need to go back to the existing code for a bug fix or enhancement, it is a one line change in the docker-compose.
Step 1: Back Up the Docker Volume
Let’s say you have a volume named db_data that you want to back up. Run this command from the terminal in your project directory:
1docker run --rm \
2 -v db_data:/source \
3 -v $(pwd):/backup \
4 busybox \
5 tar czf /backup/db_data.tar.gz -C /source .What it does?
--rm: Automatically removes the container after it finishes.-v db_data:/source: Mounts the source Docker volume.-v $(pwd):/backup: Mounts your current directory to write the backup file.busybox: A tiny Linux container that includes tar.tar czf: Creates a compressed tarball of the volume contents.
This will create a file called db_data.tar.gz in your current directory.
Step 2: Check the Backup Contents
Want to double-check what’s inside the archive?
Use:
1tar -tzf db_data.tar.gz | headThis lists the first few files inside the backup, so you know it’s not empty or corrupt.
Step 3: Move the Backup to the New Machine
Transfer db_data.tar.gz to the new machine. You can use scp, ftp, a USB drive, cloud storage, AirDrop, whatever works for you.
Example:
1scp db_data.tar.gz v@mac2:\~/DownloadsStep 4: Restore the Docker Volume
On the new machine, create an empty volume with the same name:
1docker volume create db_dataNow restore the backup into that volume:
1docker run --rm \
2 -v db_data:/restore \
3 -v \~/Downloads:/backup \
4 busybox \
5 tar xzf /backup/db_data.tar.gz -C /restoreWhat it does?
-v db_data:/restore: Mounts the target volume to write data into.-v ~/Downloads:/backup: Mounts the directory where the tarball lives.tar xzf: Extracts the archive into the volume.
Step 5: Verify the Restore
Option 1: Use Docker Desktop, Go to Volumes and explore db_data visually.
Option 2: Inspect from the terminal:
1docker run --rm -v db_data:/data busybox ls /dataThis lets you see the contents directly from the CLI.
Bonus: Use the dvbackup Script
Source code: github.com/vk4s/docker-volume-backup
If you find yourself doing this often, you might want to use the dvbackup script (I have created it), which simplifies the entire process:
- Install the script: (always check the contents of any script before running it in your system).
1curl -O https://raw.githubusercontent.com/vk4s/docker-volume-backup/main/dvbackup
2chmod +x dvbackup
3./dvbackup install- Backup volumes:
1dvbackup backup ./backups db_data postgres_data- Restore volumes (with same names):
1dvbackup restore ./backups db_data postgres_data- Restore with new names:
1dvbackup restore ./backups new_db=db_data new_pg=postgres_dataThe script provides following features:
- Automatic validation of volume names
- Safe handling (won’t overwrite existing volumes)
- Support for multiple volumes in one command
- Cross-platform compatibility (macOS, Linux, Windows Git Bash)
It’s a great tool for developers who frequently need to move Docker volumes between machines or create backups of their data.
Docker Volume Cheatsheet
Basic Volume Operations
1# Create a new volume
2docker volume create my_volume
3
4# List all volumes
5docker volume ls
6
7# Inspect a volume
8docker volume inspect my_volume
9
10# Remove a volume
11docker volume rm my_volume
12
13# Remove all unused volumes
14docker volume pruneUsing Volumes with Containers
1# Mount a volume to a container
2docker run -v my_volume:/data my_image
3
4# Mount a volume with read-only access
5docker run -v my_volume:/data:ro my_image
6
7# Mount a specific directory as a volume
8docker run -v /host/path:/container/path my_image
9
10# Use a named volume with docker-compose
11# volumes:
12# my_volume:
13# name: my_volumeVolume Backup and Restore
Checkout dvbackup above.
Volume Management
1# Check volume disk usage
2docker system df -v
3
4# Backup all volumes in a project
5docker-compose down
6dvbackup backup ./backups $(docker volume ls -q --filter name=project_name)
7
8# Migrate volumes between hosts
9dvbackup backup ./backups volume_name
10# Transfer backup file to new host
11dvbackup restore ./backups volume_nameCommon Volume Patterns
1# Database volumes
2docker volume create postgres_data
3docker run -v postgres_data:/var/lib/postgresql/data postgres
4
5# Application data
6docker volume create app_data
7docker run -v app_data:/app/data my_app
8
9# Configuration volumes
10docker volume create app_config
11docker run -v app_config:/app/config my_app
12
13# Cache volumes
14docker volume create app_cache
15docker run -v app_cache:/app/cache my_app