Keywords: Docker image migration | docker save | docker load | cross-host transfer | container deployment
Abstract: This article provides a comprehensive guide for migrating Docker images between hosts without relying on Docker repositories. Through the combined use of docker save and docker load commands, along with file transfer tools, efficient and reliable image migration is achieved. The content covers basic operational steps, advanced compression techniques, important considerations, and practical application scenarios, offering Docker users a complete migration reference.
Fundamental Principles of Docker Image Migration
Cross-host Docker image migration is a common requirement in containerized deployment, particularly in environments without access to public or private repositories. The core of image migration involves packaging the image and all its layers into a single file, transferring it between hosts using file transfer tools, and finally reloading it on the target host.
Detailed Standard Migration Process
The complete image migration process consists of three key steps: image export, file transfer, and image import.
Image Export Operation
On the source host, use the docker save command to export the specified image as a tar-format archive file. This command captures all layers and metadata of the image, generating a complete image snapshot.
docker save -o /path/to/image.tar repository/image_name:tag
In practical operation, ensure the complete image name and tag are specified. For example, for a custom image based on Red Hat Linux:
docker save -o /home/user/custom_rh_image.tar custom/redhat:latest
File Transfer Methods
The generated tar file can be transferred to the target host through various methods:
- SCP Transfer: Suitable for environments with good network conditions
- Rsync Transfer: Ideal for large file transfers with resume capability
- Physical Media: Transfer via physical devices like USB drives
Example using SCP transfer:
scp /home/user/custom_rh_image.tar user@destination_host:/tmp/
Image Import Operation
On the target host, use the docker load command to import the image from the tar file:
docker load -i /tmp/custom_rh_image.tar
After import completion, verify the image was successfully loaded using the docker images command.
Advanced Transfer Techniques
For network transfer scenarios, compression tools can be combined to improve transfer efficiency. The Docker load command supports automatic decompression and can directly process compressed image files.
Streaming Compression Transfer
Chain image saving, compression, and transfer operations through pipelines for efficient streaming processing:
docker save custom/redhat:latest | gzip | ssh user@destination_host 'docker load'
Compression Algorithm Selection
Choose appropriate compression algorithms based on network conditions:
- gzip: Suitable for high-speed network environments with fast compression
- bzip2: Provides good compression ratio for medium bandwidth
- xz: Highest compression ratio for low-speed network environments
Transfer Progress Monitoring
Use the pv tool for real-time transfer progress monitoring:
docker save custom/redhat:latest | pv | gzip | ssh user@destination_host 'docker load'
Practical Application Scenario Analysis
In enterprise environments, image migration typically faces the following common scenarios:
Custom Base Image Migration
When building custom images based on specific operating systems (like Red Hat Linux), recreating from Dockerfile may be infeasible due to complex dependencies. The save/load approach completely preserves all customizations in the image.
Legacy System Migration
For containerized systems running for years, the original build environment may no longer be available. Direct migration of existing images becomes the only viable solution, avoiding complex dependency reconstruction.
Offline Environment Deployment
In environments without internet or internal repository access, transferring image files via physical media becomes a necessary deployment method.
Important Considerations and Best Practices
Permission Management
Docker operations typically require administrator privileges, add sudo before commands:
sudo docker save -o image.tar repository/image_name:tag
sudo docker load -i image.tar
Image State Consistency
When exporting images from running containers, it's recommended to stop the container first to ensure filesystem consistency. While Docker commit can capture running state, it doesn't guarantee all changes are completely saved.
Version Compatibility
Image compatibility between different Docker versions is generally good, but testing target environment compatibility before migration is advised. Migration from Docker 18.x to 23.x typically doesn't encounter issues.
Security Considerations
When migrating older version images, be aware of security risks that may contain unpatched vulnerabilities. It's recommended to update base images and security patches promptly after migration.
Performance Optimization Recommendations
Storage Space Management
Image tar files may consume significant disk space; clean temporary files promptly after transfer completion. Using compressed transfer reduces network bandwidth consumption.
Network Transfer Optimization
For large file transfers, rsync is more suitable than scp, supporting incremental transfer and resume capability:
rsync -avz --progress image.tar user@destination_host:/tmp/
Conclusion
Through the combination of docker save and load commands, coupled with appropriate file transfer tools, efficient and reliable cross-host Docker image migration can be achieved. This method doesn't rely on external repositories and is applicable to various network environments and deployment scenarios. In practical operations, appropriate transfer methods and compression strategies should be selected based on specific requirements, while paying attention to key factors such as permission management, version compatibility, and security.