Copying Docker Images between Hosts

When it comes to copying or moving images, Docker makes it very easy to push your custom images to a public or a private repository on Docker Hub, and this will make them available for future use. However, you might sometimes want to just copy and paste your images to be used on another host that is running on the Cloud or on-premise. A common use case would be a host that simply does not have Internet access to download images from Docker registries.

In this short post, we will go through the steps on how to copy some Docker images between two hosts running in the same local network.

Copying One Image at a Time


Step 1: Save the image as .tar file on the source host

List the exiting images on the host

$ docker images
REPOSITORY                TAG            IMAGE ID            CREATED             SIZE
php                       latest         4a05f96f44b5        4 days ago          367MB
phpmyadmin/phpmyadmin     latest         126b8717cebb        4 days ago          166MB
wordpress                 latest         e2c4085bbc2b        12 days ago         408MB
mysql                     latest         29e0ae3b69b9        12 days ago         484MB
httpd                     latest         11426a19f1a2        3 weeks ago         178MB
ubuntu                    latest         735f80812f90        4 weeks ago         83.5MB
ubuntu                    14.04          971bb384a50a        5 weeks ago         188MB
hello-world               latest         2cb0d9787c4d        6 weeks ago         1.85kB
ansible/centos7-ansible   latest         688353a31fde        20 months ago       447MB
janes/alpine-lamp         latest         40262259deda        21 months ago       336MB

Save the image you want to copy and provide the destination path. In my case, I will save wordpress needs to be saved in the /tmp folder. No output is displayed if the operation succeeds.

$ docker save wordpress -o /tmp/wordpress.tar

Check that the wordpress.tar file is in the destination path

$ ls -la /tmp/wordpress.tar
-rw-------. 1 user user 418163712 Aug 27 10:01 /tmp/wordpress.tar

Step 2: Copy the image to the destination host

Once the image is saved, use rsync or scp to copy the .tar file to the host you want the resulted image to be imported. I’m using scp to copy it over a destination host named dest-srv.

$ scp /tmp/wordpress.tar dest-srv:/tmp
user@dest-srv's password:
wordpress.tar 

Step 3 Load the image on the destination host

No image is currently present on the destination server.

[user@dest-srv ~]$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE

Run the following command to import the image.

$ docker load -i /tmp/wordpress.tar

If everything goes fine, you’ll get the following output

cdb3f9544e4c: Loading layer [==================================================>]  58.44MB/58.44MB
31438e5d43ea: Loading layer [==================================================>]  3.584kB/3.584kB
3e38617eea69: Loading layer [==================================================>]  213.5MB/213.5MB
479a0a84592c: Loading layer [==================================================>]  3.584kB/3.584kB
1da1b9a6c4bf: Loading layer [==================================================>]  43.04MB/43.04MB
872913a83f61: Loading layer [==================================================>]  10.24kB/10.24kB
b1797d3ad709: Loading layer [==================================================>]  9.728kB/9.728kB
735ec19c63e8: Loading layer [==================================================>]  4.096kB/4.096kB
6369726996f7: Loading layer [==================================================>]   7.68kB/7.68kB
d82082800a51: Loading layer [==================================================>]  13.51MB/13.51MB
81ea784bf5a0: Loading layer [==================================================>]  4.096kB/4.096kB
33a57a5523f9: Loading layer [==================================================>]  57.41MB/57.41MB
213442d32d61: Loading layer [==================================================>]  11.78kB/11.78kB
2136571befde: Loading layer [==================================================>]  4.608kB/4.608kB
e86d5853a911: Loading layer [==================================================>]  4.608kB/4.608kB
ceba4df956dd: Loading layer [==================================================>]   3.57MB/3.57MB
7b4d018c2b92: Loading layer [==================================================>]  4.608kB/4.608kB
a495ffad4bdc: Loading layer [==================================================>]  7.168kB/7.168kB
3c6e8322db53: Loading layer [==================================================>]  28.51MB/28.51MB
c5781c63d196: Loading layer [==================================================>]  10.75kB/10.75kB
Loaded image: wordpress:latest

Once imported, the Docker images will be visible with the docker images command.

[user@dest-srv ~]$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
wordpress           latest              e2c4085bbc2b        12 days ago         408MB

You can now use your image or import other images as per your need.

 

Copying All Images in Bulk


The previous method saves only one image at at a time. If you are sure you want to use all your existing images, you can save them in bulk using the following the command:

$ docker save $(docker images -q) -o /pathToSaveAllDockersimages.tar

To save all the images in the ~/images/ folder as allimages.tar file, the above command will help. Again, no output is displayed if the command succeeds.

$ docker save $(docker images -q) -o ~/images/allimages.tar
$
$ ls ~/images
allimages.tar  lost+found

Copy the allimages.tar file to the destination server, then use the same command to import all the images.

[user@dest-srv]$ docker load -i ~/images/allimages.tar
Loaded image ID: sha256:735f80812f90aca43213934fd321a75ef20b2e30948dbbdd2c240e8abaab8a28
Loaded image ID: sha256:2cb0d9787c4dd17ef9eb03e512923bc4db10add190d3f84af63b744e353a9b34
Loaded image ID: sha256:688353a31fdee02a966d1f83e9210f77b5a63baaaacbedb81ca35f6231cfeb6c
Loaded image ID: sha256:4a05f96f44b57b3a6769520e5535f8da8d2f0c025ecf25fca153a1cb4b44e0ac
Loaded image ID: sha256:e2c4085bbc2bb988bdb87e73f63be1048799385a6ccff6acd7b36233475c9278
Loaded image ID: sha256:11426a19f1a28d6491041aef1e1a7a2dcaa188d0165ae495de7d8fc1bf3e164f
Loaded image ID: sha256:126b8717cebbb182a3cbb7d3dda681cca4e23a657f0eb236dfd028b159c8cfec
Loaded image ID: sha256:971bb384a50ae93856362f9c40ea471e66a97cfd4023b9cc9b8e4196e99da2c7
Loaded image ID: sha256:40262259dedad574a8cd258fb4cbfc75ccc09fe76fd2fd049479d47afc6d75f1

Checking that all images have been imported on the destination host.

[user@dest-srv]$ docker images
REPOSITORY                TAG                 IMAGE ID            CREATED             SIZE
php                       latest              4a05f96f44b5        4 days ago          367MB
phpmyadmin/phpmyadmin     latest              126b8717cebb        4 days ago          166MB
wordpress                 latest              e2c4085bbc2b        12 days ago         408MB
httpd                     latest              11426a19f1a2        3 weeks ago         178MB
ubuntu                    latest              735f80812f90        4 weeks ago         83.5MB
ubuntu                    14.04               971bb384a50a        5 weeks ago         188MB
hello-world               latest              2cb0d9787c4d        6 weeks ago         1.85kB
ansible/centos7-ansible   latest              688353a31fde        20 months ago       447MB
janes/alpine-lamp         latest              40262259deda        21 months ago       336MB

As you can see, moving images between Docker hosts is amazingly easy just by using two commands. This procedure is possible whether the hosts are on-premise or on the cloud, making images manipulation on isolated servers very flexible.

Leave a Comment

Your email address will not be published. Required fields are marked *