If you have a need to move Docker containers from one host to another, here's how to do that with the export/import feature.
You have Docker running smoothly and you've created a number of containers on your server. But what happens when you want (or need) to move one or more of those containers from one host to another? You could always push the image they were based on to Docker Hub, pull the image down, and rebuild the container. Or you could simply export the container, move the exported file to the machine in question, and import that container.
Sounds easy, right? The good news: it is. 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. This task could come in very handy, especially if your Docker server is starting to fail or you simply want to migrate your containers to more powerful hardware.
Regardless of why, let's walk through the process.
Listing your containers
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 (Figure A). 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. Congrats, the container has been imported. To exit from the container, issue the command exit.
Easy container move
Using the export and import feature of Docker is one of the easiest ways to move a container from host to host. It's also a great way to backup your containers (think "shell script" here). But that's another topic for another day.
Enjoy moving your containers around!
- How to install and use Docker on Windows (TechRepublic)
- How to run cleanup tasks with Docker using these 4 tips (TechRepublic)
- How to spin up a quick Docker test platform with boot2docker (TechRepublic)
- How to start working with Docker logs (TechRepublic)
- Learn Python with 70+ Exercises (TechRepublic Academy)
- Weak Docker security could lead to magnified vulnerabilities due to efficiency of containers (ZDNet)