Data Centers

How to export and import containers with Docker

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.

dockerhero.jpg
Image: Jack Wallen

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.

Figure A

Figure A

A list of Docker containers by name.

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!

Also see

About Jack Wallen

Jack Wallen is an award-winning writer for TechRepublic and Linux.com. He’s an avid promoter of open source and the voice of The Android Expert. For more news about Jack Wallen, visit his website jackwallen.com.

Editor's Picks

Free Newsletters, In your Inbox