How to move Docker containers from one host to another?

0 votes
I want to move one or more of the docker containers from one host to another. One way that i can think of is that, You could always push the image of the container on to Docker Hub, pull the image down, and rebuild the container. Is there any other way that i could do it, without using the docker save command?
Jul 2 in Docker by Sam
• 1,560 points
81 views

1 answer to this question.

0 votes

Yes you could definitely do it the other way using the export /import commands in docker.Let me walk you through the steps of exporting and importing your containers, so that you can rest assured that those containers can be even more portable than you thought. 

The first thing you need to do is list out your containers. You'll be exporting them by name, so you need to know the names of those containers. To do this, issue the command:

docker ps -a​ 

The resulting output will include the NAME column,It is this column where you will get your names for exporting.

Exporting a container

For ease of transport, we'll be exporting the containers into a gzipped file. The command to export the containers is:

docker export NAME | gzip > NAME.gz

Where NAME is the name of the container to be exported. You are now ready to relocate the file and import it.

Importing a container

In similar fashion to the export, we're going to import the container with a single command. Obviously, before you do this, you must first move the exported file to the new server. You could do this using the scp command like so:

scp NAME.gz USERNAME@SERVER_IP:/home/USERNAME

Where NAME is the file name, USERNAME is the user name on the remote server, SERVER_IP is the IP address of the remote server, and USERNAME is (again) the name of the user on the remote server.

Once you've done that, the import can be handled with the following command:

zcat NAME.gz | docker import - NAME

Where NAME is the name of the container. You can now run the newly imported container with a command similar to:

docker run -i -t NAME /bin/bash

Where NAME is the name of the container.

You should find yourself within the container's command prompt, where you can work on the container. Now, the container has been imported. To exit from the container, issue the command exit.

Using the export and import feature of Docker is one of the easiest ways to move a container from host to host.Enjoy moving your containers around!

Note: The docker export command does not export the contents of volumes associated with the container. If a volume is mounted on top of an existing directory in the container, docker export will export the contents of the underlying directory, not the contents of the volume.

answered Jul 2 by Sirajul
• 25,700 points

Related Questions In Docker

0 votes
1 answer

How to obtain the Docker container's IP address from the host?

This can be done by executing the ...READ MORE

answered Jul 16, 2018 in Docker by Sophie may
• 9,530 points
407 views
+4 votes
4 answers

How To Access a Service on Host From a Docker Container?

Adding to kalgi's answer, You can also ...READ MORE

answered Oct 16, 2018 in Docker by lina
• 8,110 points

edited Oct 16, 2018 by lina 4,287 views
0 votes
1 answer

How to get docker-compose to always re-create containers from fresh images?

The containers are getting recreated to preserve ...READ MORE

answered Jul 27, 2018 in Docker by Kalgi
• 40,460 points
449 views
+1 vote
1 answer

How to expose docker container's ip and port to outside docker host without port mapping?

you can accomplish this with IP aliasing ...READ MORE

answered Aug 2, 2018 in Docker by Kalgi
• 40,460 points
2,148 views
0 votes
1 answer
0 votes
1 answer

Deploy Docker Containers from Docker Cloud

To solve this problem, I followed advice ...READ MORE

answered Sep 3, 2018 in AWS by Priyaj
• 56,520 points
156 views
0 votes
2 answers