Export and Import a Docker Image Between Nodes

Export and Import a Docker Image Between Nodes

Get Social!

docker-logoOne of the driving forces behind Docker is to create a consistent environment across all Docker enabled machines and to create portable templates, or images, which can be ran on any Docker enabled server.

It would, therefore, make perfect sense that Docker have made it very easy for us to export a running container and re-import it on another Docker server.

Lets assume, for this example, that you have a running container that you would like to move to another host. The summary of the process is to save the container to an image, save it to a tar file, move it to your new host and load the image into the new docker server.

Find the ID of the container that you would like to move.

$ docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
f4b0d7285fec        ubuntu:14.04        /bin/bash           38 minutes ago      Exit 0                                  hungry_thompson
8ae64c0faa34        ubuntu:14.04        /bin/bash           41 minutes ago      Exit 0                                  jovial_hawking
3a09b2588478        ubuntu:14.04        /bin/bash           45 minutes ago      Exit 0                                  kickass_lovelace

I’m going to use the above 3a09b2588478 ID for this example.

Commit your changes and save the container to an image called mynewimage.

$ docker commit 3a09b2588478 mynewimage
4d2eab1c0b9a13c83abd72b38e5d4b4315de3c9967165f78a7b817ca99bf191e

Save the mynewimage image to a tar file. I will use the /tmp/ directory to save the image but you could easily use a NFS share to make it easier to move the completed tar file.

$ docker save mynewimage > /tmp/mynewimage.tar

Copy the mynewimage.tar file to your new Docker instance using whatever method works in your environment, for example FTP, SCP, etc.

Run the docker load command on your new Docker instance and specify the location of the image tar file.

$ docker load < /tmp/mynewimage.tar

Finally, run the docker images command to check that the image is now available.

$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
mynewimage          latest              4d2eab1c0b9a        5 minutes ago       278.1 MB
ubuntu              14.04               ad892dd21d60        11 days ago         275.5 MB
<none>              <none>              6b0a59aa7c48        11 days ago         169.4 MB
<none>              <none>              6cfa4d1f33fb        7 weeks ago         0 B


13 Comments

Luc

5-Sep-2014 at 3:34 pm

Thanks for this tutorial, this is exactly what I needed.

Nico

10-Sep-2014 at 6:24 am

How to load an image with preserving it’s repository and tag name?

    Alex Glover

    8-Jan-2015 at 4:48 pm

    If you use the “docker save” command, repository and tag name should be stored as part of the tar file. When you execute “docker load” it should retain the repo and tag name.

Thomas

11-Mar-2015 at 2:16 pm

Hey!

Thank you very much for this tutorial. I have one problem, you may know to solve. I have two hosts with fresh docker installations. On host A is a running container with changes, so I commited and saved it along your tutorial, copied to host B and do “docker load < /tmp/whatever.tar". After that it seems it doing something and finishing. Run "docker ps -a" afterwards, nothing is listed. Indeed there isn't any image or container on host B before! What can I do?

    Brad

    13-Mar-2015 at 10:07 pm

    “docker load” creates an image, not a container. “docker ps -a” will show all containers, not images. “docker images” should show you the image you created with “docker load”.

twodayslate

15-Oct-2015 at 5:07 pm

If you don’t want to use STDIN/STDOUT use the following:

docker save -o ~/image.tar
docker load -i ~/image.tar

buriz

19-Nov-2015 at 3:07 pm

We have a problem when saving docker images and loading them on another machine (running the same OS – CoreOS) not all files are the same. Some symlinks are lost within apache directory.

1. $ docker commit [container ID] [image_name]
2. $ docker save > [image_name].tar
3. Copying the image on another machine running exactly the same OS (In fact it is a cloned VM, so it is exactly the same).
4. $ docker load < [image_name].tar
5. $ docker run –name [container_name] –link [link_wordpress_to_db]:mysql -e WORDPRESS_DB_USER=[user] -e WORDPRESS_DB_PASSWORD=[pass] -e WORDPRESS_DB_NAME=[db_name] -p 8080:80 -d [image_name]

For some reason saving and loading the image does not retain the symlinks from /var/www/html properly. Even their ownership is still www-data, although in the original container they are owned by root and [wp-user].

varaprasad

25-Feb-2016 at 9:44 am

what is the different between EXPORT and SAVE

Pratik Bandarkar

24-Feb-2017 at 11:43 am

What is the use of `docker export/import` ?

Rajesh

7-Mar-2017 at 4:03 am

Thanks for your tutrorial.I have learned how to do it.Thanks

Harshul

23-Aug-2017 at 4:09 am

Very clean and crisp tutorial. Thank you!

babuni

14-May-2018 at 1:37 pm

How to add more Images on same existing file???

shahriar entezam

30-Jun-2018 at 4:06 pm

thanx a lot

Leave a Reply

Visit our advertisers

Quick Poll

How many Proxmox servers do you work with?

Visit our advertisers