logo of Akmatori
05.01.2025

How to Transfer All Local Docker Images from One Host to Another

head-image

Migrating Docker images between hosts can be a critical task during system upgrades, server migrations, or when scaling infrastructure. This guide will walk you through the process step-by-step, ensuring that your images are transferred efficiently and without issues.

Why Transfer Docker Images?

Moving Docker images allows you to:

  • Preserve application environments.
  • Minimize downtime during migrations.
  • Avoid re-downloading large images from registries.
  • Save bandwidth and time in restricted environments.

Step 1: List All Docker Images on the Source Host

First, identify the images you need to transfer. Run the following command:

docker images  

This will display all local images with their names, tags, and sizes.

Step 2: Save Docker Images to TAR Files

Docker allows you to export images as .tar files using the docker save command. To export all images, loop through the list of images:

docker images --format "{{.Repository}}:{{.Tag}}" | while read image; do  
  docker save "$image" -o "$(echo $image | tr /: _).tar"  
done  

This creates .tar files for each image. For example, nginx:latest becomes nginx_latest.tar.

Batch Export (Optional)

If disk space is limited, save only the necessary images by listing their names manually or using filters:

docker save nginx:latest -o nginx_latest.tar  

Step 3: Transfer TAR Files to the Target Host

Transfer the .tar files using any preferred method:

  • Option 1: Secure Copy (SCP)

Use SCP to transfer the files securely:

scp *.tar user@target-host:/path/to/destination  
  • Option 2: rsync

Rsync is efficient for large files or slow networks:

rsync -avz *.tar user@target-host:/path/to/destination  
  • Option 3: USB Drive or External Storage

If both hosts are in the same physical location, copy the files to external storage, then transfer them to the target host.

Step 4: Load Docker Images on the Target Host

On the target host, load the images into Docker using the docker load command:

cd /path/to/destination  
for tarfile in *.tar; do  
  docker load -i "$tarfile"  
done  

Each image will be loaded into the local Docker environment. You can verify this by listing images:

docker images  

Step 5: Test the Transferred Images

Run a container from one of the loaded images to ensure everything works as expected:

docker run --rm -it <image-name>:<tag>  

If the container starts successfully, the image was transferred and imported correctly.

Automating the Process

For frequent migrations or backups, create a script to handle the process:

Script for Exporting and Transferring

#!/bin/bash  
DESTINATION="user@target-host:/path/to/destination"  

docker images --format "{{.Repository}}:{{.Tag}}" | while read image; do  
  tarfile="$(echo $image | tr /: _).tar"  
  docker save "$image" -o "$tarfile"  
  scp "$tarfile" "$DESTINATION"  
done  

Script for Importing

Run this script on the target host:

#!/bin/bash  
for tarfile in /path/to/destination/*.tar; do  
  docker load -i "$tarfile"  
done  

Tips for Large Transfers

  • Compression: Reduce file sizes with gzip:

    docker save nginx:latest | gzip > nginx_latest.tar.gz  
    

    Then load the compressed file:

    gunzip -c nginx_latest.tar.gz | docker load  
    
  • Parallel Transfers: Use tools like parallel to transfer multiple files simultaneously.

  • Disk Space Management: Ensure the target host has enough disk space for all images.

Conclusion

Transferring Docker images between hosts is straightforward with the docker save and docker load commands. Whether you're migrating to a new server or sharing images, following these steps will ensure a smooth process.

Take your container reliability to the next level by trying Akmatori. Akmatori predicts failures and assists developers in building reliable systems. Accelerate your incident resolution today!

Thanks for reading! If you found this guide helpful, let us know. Happy Dockerizing!

Maximize your website or application's performance and reliability!