Docker users may sometimes need to move Docker containers from one server to another. With Docker export and import tools, migration of containers can be easily performed.
Here at Ibmi Media, as part of our Docker support services, we regularly help our customers to perform Docker related tasks.
In this context we shall look into how export and import of docker containers are done.
Why is exporting and importing docker containers important?
As earlier stated, there are many factors that might trigger the movement of docker containers between one host or server to another. For instance, there docker server might fail to start or you may need to migrate the containers to a more efficient server.
To achieve this, we can actually push a docker image from where it was hosted to a Docker hub, pull the image down and rebuild the container on a different host. Alternatively, you can simply export the container, move the exported file to the server or host which is its destination and then import the container accordingly.
How to export a docker container?
To start with, you need to list the containers available in the host to know the container which you will export. To achieve this, simply run the command below;
docker ps -a
The names of the available containers will be listed under the NAMES column which you can in turn use for exporting.
To export a container easily, you can export the container as a gzipped file format with the command below;
docker export FILE | gzip > FILE.gz
Here "FILE" represents the name of the container to be exported.
How to import a docker container?
To import a container to a new server, you need to move the exported container to the new server and use the "SCP" command as shown below;
scp FILE.gz USERNAME@SERVER_IP:/home/USERNAME
Here "USERNAME" represents the user name on the destination server while "FILE" is the name of the file.
To import this file, run the command below;
zcat FILE.gz | docker import - FILE
To run the imported container, use the command below;
docker run -i -t FILE /bin/bash
Finally, you can use the exit command to exit from the container.