Move Docker Container to another host
In working with Docker containers, you will inevitably have a use case where you need to move your docker container from one host to another or back up the container to bring it back onto a host after a rebuild or another event. Let’s look at the commands for moving Docker images and Docker containers.
Why move your Docker containers or Docker images?
We have alluded to many reasons at the outset, but let’s consider a few quick use cases for moving or migrating Docker containers. The very nature of a container and its benefits are with its portability. Generally speaking, everything you need to run your application is contained in the container.
So you should be able to move your Docker image to another host machine or your Docker container and have that container image run on a different host. You may need to upgrade a container host or perform maintenance, or you may want to move your containers to a different host altogether.
These are only a few of the reasons that may come up. However, whatever the case, moving your container is the same. Let’s consider the Docker commands you want to be familiar with to move your containers, container images, and data to another host.
Docker commands to move Docker image and Docker container
You want to be familiar with a few key commands when moving container images and containers from one host to another. Note the following:
docker export command
docker save command
docker commit command
docker volumes-from command
One point to make, the commands to export or save containers and images do not take the Docker network configuration to the new host. You must ensure your appropriate Docker networks are defined on the new server.
Docker export command
The docker export command is an essential command you will want to know to move Docker containers from one host to another. The docker export command can export a container to a tar file (compressed file). So, it is not the container image. So, with the export command, you are grabbing the container running from a certain image, not the base image. Moving it to a new server will contain any file changes, etc., from the old server.
Difference from an image
It does not contain image layer information, including all history and metadata. This is captured with the docker save command, which we will cover next. Using the docker export command, you can copy your resulting .tar file to your remote server and use this to import your container on the new host. YOu can use the following command:
docker export <container name> /path/to/file.tar
You can then take this exported file and copy it to your remote server, using the docker import command to move docker container to another host.
Drawbacks to the export command
There is a significant drawback to the docker export command. It does not copy ports and variables for docker network configuration. In addition, it doesn’t copy over the underlying data volume, which contains the container data.
Docker save command
The docker save command allows you to save a Docker image to a compressed file in the .tar format. The syntax is very similar to the docker export command. As we mentioned, the docker export command is not an export of the image, but rather of the container.
For moving container images, you use the docker save command. It also allows you to move from one registry to another. Note the single command below.
docker save <container IMAGE> /path/to/file.tar
For the docker save command, you can use the container ID or the friendly name of the container.
Due to the limitation of the docker export command not backing up the ports, etc, using the docker save command is the most commonly used method as it captures
Docker commit command
The docker commit command commits a container’s file changes or settings into a new image. This can be used in conjunction with the docker save command. If you want to create a new image from an existing container, the docker commit command can be used to do this.
The command will pause the container while the image is committed to helping ensure data consistency and reduce the likelihood of data loss or corruption. You can configure the pause behavior with a parameter.
docker commit <container id> mycontainer/testimage:version1
The docker volumes command allows working with docker volumes. Docker volumes are a native feature of Docker that allows attaching local filesystem locations to your docker container.
The benefit of the docker volume is it allows you to persist data on your Docker host. In this way, your data is immutable whereas your container can be spun up and spun down as needed without affecting the data.
The docker –volumes-from command is handy as it allows creating a container as a template for mounting a set of volumes to a new container. This is a handy feature as we can create a container with the volume mounts when used with the docker run command.
docker run --volumes-from <container id>
We can create a container from an existing directory on the container host and the image linked with the volume mounts.
Unless explicitly defined, the volume mounts are typically found in the var lib docker folder. This is where Docker mounts the data volumes on the Docker host.
The docker load command allows loading an image from a tar archive or STDIN which is handy if we have saved a container image to a tar file. We can then use the docker load command to load an image or repository. It restores the images and tags.
docker load mycontainer.tar or sudo docker load
Example of move docker container to another host
In the below, I am going to move my Pialert container from one host to another. Let’s look at an easy example of moving an nginx container from one host to another. So, we are pulling the latest nginx container in the example below and running it.
Next, we take a look at the docker images.We can see the nginx image listed. After that, we are using the docker save command to compress the image to a .tar file. Finally, we use scp to copy the tar file to the destination docker host.
On the destination docker host, we use the command cat nginx.tar | docker load to load the docker image.
If we look at the images we have available now, we will see the nginx image.
Finally, we run our container from the loaded image. As you can see below, I changed the host port since I didn’t have port 80 available on the destination host.
Backing up volumes
You can backup a Docker container with volumes included using the command:
docker run --rm --volumes-from <container name> -v $(pwd):/backup busybox tar czvf /backup/container.tar <container volume mount>
Hopefully, this tutorial on the commands and how to move docker container to another host shows the options available for moving your containers between hosts. Many robust commands are available on the docker side to handle various moves, migrations, and other data-related scenarios.