Docker images can be compressed, exported, and imported using the Docker command line tools. Here are the detailed steps:
-
Compress and Export the Docker Image
Use the
docker save
command to compress the Docker image into a tar file, for example:1
docker save -o my_image.tar my_image:tag
This command saves the image named
my_image
with the tagtag
as the filemy_image.tar
. -
Copy the Docker Image Tar File to Another Host
Use the
scp
command or other file transfer tools to copy the Docker image tar file to another host, for example:1
scp my_image.tar user@new_host:/tmp/
This command copies the
my_image.tar
file to the/tmp/
directory on the target host. -
Import the Docker Image
Use the
docker load
command to import the Docker image on the new host, for example:1
docker load -i /tmp/my_image.tar
This command extracts and imports the image file into the Docker engine.
-
Verify the Docker Image
After the import is complete, you can use the following command to verify if the Docker image has been successfully imported:
1
docker images
This command lists all the images present in the local Docker engine, confirming that the required Docker image has been imported.
If the image is too large, you can compress it before exporting and then import it.
Image Compression for Export and Import
You can compress the image using the gzip tool:
|
|
To decompress and import, use gunzip, which is typically bundled with most systems:
|
|
In summary, exporting a Docker image requires using the docker save
command to compress and save it, while importing a Docker image necessitates the docker load
command for decompression and import. These commands facilitate the easy migration of applications between multiple Docker hosts.