How To Migrate Docker Containers To New Server

Docker is the simplest way to deploy a container image that contains a functional operating system and uses the host kernel.

We showed you how to install and use Docker containers in this post. Now we will show you how to migrate Docker containers to a new server.

It is straightforward to set up and manage Docker containers. You can easily create a virtual operating system with just a few commands from hundreds of containers on the Docker hub.

To deploy a docker image, you first need to download the source from Docker Hub. Then, the command docker create downloads automatically and deploys the specified container image.

If we decide to migrate our Docker container to a new server, first, we need to save the operating container as an image and move it to the new server, then load the image as a new container.

Another method to move the docker containers is the export and import.

The export method is different than the saving method because the export method creates a snapshot of a running container. In contrast, the save command method generates an image that you can use to create a container.

The following commands will let you move your Docker containers to the new server.

Things to remember before migrating the docker container

Using the docker export and docker import command, the resulting tar file that you moved to the new server won’t transfer the ports and variables. So you will need to open the ports and create environment variables manually.

Second, the docker export and docker save commands do not backup the container volumes.

How to migrate Docker containers to a new server

In our example, we will migrate an apache server by using the docker save command. First, we create an image of the running container. We suggest you stop the container before starting the process.

Suppose your server is in production mode and your application generates a lot of data. In that case, we recommend you to put it in maintenance mode to avoid data loss during the image creation.

We are going to use the docker commit command to create the image:

sudo docker commit container_id image_name

Use the docker ps command to get the container id and replace container_id with the one you just got from using the docker ps command.

Next, replace image_name with any name you want. In our example, we will replace it with apache-image because we migrate an apache server.

[email protected]:~$ sudo docker ps
CONTAINER ID  IMAGE        COMMAND            CREATED         STATUS        PORTS                                    NAMES
434ccd9f15a4  httpd:latest "httpd-foreground" 10 seconds ago  Up 9 seconds  0.0.0.0:49153->80/tcp, :::49153->80/tcp  apache
sudo docker commit 434ccd9f15a4 apache-image
[email protected]:~$ sudo docker commit 434ccd9f15a4 apache-image
sha256:22d7b415fe967b3ff67bfde21846a8dcb267d161370699e5c42784547966f73f

Now we created the container image by using the docker commit command, we can save it in tar file by using docker save command.

sudo docker save image_name > image_name.tar

Now you need to replace image_name with the name of the image created when we used the docker commit command and the same for image_name.tar replace it with any name you want to have the saved image archive.

In our example, we migrate an apache server so that we will name it apache_image.tar.

sudo docker save apache-image > apache-image.tar
[email protected]:~/images$ sudo docker save apache-image > apache-image.tar
[email protected]:~/images$ ls
apache-image.tar

Once the image is created, we can transfer this image to the new server using FTP or SCP.

Transferring the image using SCP

SCP apache-image.tar [email protected]

Follow this article for more information on transferring data from a local to a remote server.

Now that we transferred the image to the remote server, we need to ssh into the new server and use the docker load command to load the image.

sudo docker load < image-name.tar

Again replace image-name.tar with the name of the transferred image. When you load the image, make sure you ssh into the directory where the image is stored. If not, provide the full path to the image.

Now use the docker run command to run the container but don’t forget to include ports and variables when you run the command.

docker run -d --name container_name -p 80:80 image-name

If your container depends on the volume, you will need to migrate that manually to the new server. Docker store the persistence data inside /var/lib/docker, and you will need to transfer that folder or the specific container directory (volume) to the new server. Now use the docker run command and attach the volume to the container.

docker run --rm -p 80:80 --name container_name -v /source:/target image-name

The -v parameter allows you to attach the existing directory (volume) to the container. If your container does not require volume, then don’t use -v parameter.

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.