Introduction
In this guide, I’ll show you how to export a Docker volume and copy it to another machine. This process involves creating a backup (tarball) of the volume, transferring it to the target machine, and extracting the content into a new volume on the target machine. We’ll use a PostgreSQL container as an example.
Step 1: Create a Container to Access the Volume
First, create a temporary container to access the volume and create a tarball containing the volume’s contents.
Run the following command in the terminal:
docker run --rm -v drupal_postgres_data:/volume -v $(pwd):/backup busybox tar czvf /backup/volume_backup.tar.gz -C /volume .
Explanation:
docker run
: Runs a Docker container.--rm
: Automatically removes the container after it completes its task.-v drupal_postgres_data:/volume
: Mounts thedrupal_postgres_data
volume to the/volume
directory in the container.-v $(pwd):/backup
: Mounts the current directory to the/backup
directory in the container.busybox
: A lightweight Docker image with basic Linux utilities.tar czvf /backup/volume_backup.tar.gz -C /volume .
: Creates a gzip-compressed tarball of the/volume
directory.
Step 2: Copy the Tarball to the Local Filesystem
The tarball is now in your current directory. Use the scp
command to copy this file to the target machine:
scp volume_backup.tar.gz user@target_machine:/path/to/destination
Explanation:
scp
: Securely copies files over SSH.volume_backup.tar.gz
: The tarball file to copy.user@target_machine
: Username and address of the target machine./path/to/destination
: Destination path on the target machine.
Step 3: Transfer the Tarball to the Target Machine
Ensure you have access rights and that the destination directory exists on the target machine.
Step 4: Extract the Tarball on the Target Machine
On the target machine, create a new Docker volume and extract the tarball into it.
1. Create a new Docker volume:
docker volume create drupal_postgres_data_new
2. Run a container to extract the tarball into the new volume:
docker run --rm -v drupal_postgres_data_new:/volume -v /path/to/destination:/backup busybox tar xzvf /backup/volume_backup.tar.gz -C /volume
Explanation:
docker run
: Runs a Docker container.--rm
: Automatically removes the container after it completes its task.-v drupal_postgres_data_new:/volume
: Mounts thedrupal_postgres_data_new
volume to the/volume
directory in the container.-v /path/to/destination:/backup
: Mounts the directory with the tarball to the/backup
directory in the container.busybox
: The Docker image used.tar xzvf /backup/volume_backup.tar.gz -C /volume
: Extracts the tarball into thedrupal_postgres_data_new
volume.
Step 5: Use the New Volume in Docker Compose on the Target Machine
Edit the docker-compose.yml
file to use the new volume:
version: '3.1'
services:
db:
image: postgres:14.2-alpine
restart: always
ports:
- "5430:5432"
environment:
- POSTGRES_DB=postgres
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
volumes:
- drupal_postgres_data_new:/var/lib/postgresql/data
networks:
- drupal-nw
volumes:
drupal_postgres_data_new:
networks:
drupal-nw:
Then, restart the Docker Compose service:
docker-compose up -d
This command starts the services defined in the docker-compose.yml
file.
Conclusion
By following these steps, you can successfully export a Docker volume from one machine and import it to another. This method ensures that the volume’s contents are accurately transferred and restored on the target machine.