Step-by-Step Guide to Migrate Docker Containers
Table of Contents
Docker makes it simple to deploy container images that contain a fully functional operating system and make use of the host kernel. We have already written an article covering the installation and use of Docker containers. In this post, we will go through how to migrate Docker containers from one server to another server.
Docker containers are relatively easy to set up and manage. We can find hundreds of containers on docker hub and create a full-fledged virtual operating system with just a few commands from the console.
In the last post in the Docker category, we learned how to host several websites or applications on a single server by using docker containers. The following commands will allow you to move your Docker containers to a new server. But first, let’s take a quick look at how we’re going to go about accomplishing this.
Typically, we deploy a docker image by first downloading it from a source like Docker Hub. The command docker create automatically downloads and deploys the specified container image.
After successfully deploying an image and publishing our application. During the production time, the application creates data, and we also install packages that modify the base docker image. When migrating docker containers, we must save the operating container as an image, transmit it to the new server, then load the docker image as a brand new container.
Another method of migrating docker containers is to export and import docker containers. Exporting a docker container using docker export
command is a little different from saving a docker image using docker save
command. docker export
command creates a snapshot of a running container whereas docker save
commands creates an image that can be used to create container. We can use both the commands to migrate our containers to a new server.
Things to remember before migrating docker container
When we use docker export
command, and import the resulting tar file to a new server, it will not transfer the ports and variables from the old system. So you will need to manually open ports and create environment variables. Secondly, docker export
and docker save
commands do not backup container volumes.
Transfer volumes separately before migrate docker container
With both the commands, we need to transfer container volumes separately to the new server. Do not worry, we can do that easily.
How to migrate docker containers to a new server
In this article, we will use docker save
command to migrate an apache server running inside a docker container. As I mentioned above, docker save
works with docker images. First we need to create image of the running container. It is preferred to stop the container before starting the process. If your server is in production and generating lots of data, you can put it on maintenance mode to avoid any data loss during the image creation.
To create image, we will use docker commit
command.
sudo docker commit container_id image_name
Replace image_name with any name that you want. For example, I am migrating an apache container, so I have decided to name the container image to be apache-image.
sandy@LinuxAndMint:~$ 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
sandy@LinuxAndMint:~$ sudo docker commit 434ccd9f15a4 apache-image sha256:22d7b415fe967b3ff67bfde21846a8dcb267d161370699e5c42784547966f73f
Now that the docker commit
command has created our container’s image, we can save it in tar file using docker save
command.
sudo docker save image_name > image_name.tar
docker commit
command. Replace image_name.tar with any name you want to give to the saved image archive. For example, in my case, I am migrating an apache server running in the docker, I have decided to name the archive as apache_image.tar.sudo docker save apache-image > apache-image.tar
sandy@LinuxAndMint:~/images$ sudo docker save apache-image > apache-image.tar sandy@LinuxAndMint:~/images$ ls apache-image.tar
Once the image has been created, we can transfer this image to the new server using ftp or scp.
Transferring the image using scp
scp apache-image.tar username@192.168.1.29
For more information on transferring data from local to remote server, follow this article.
Load the image on remote server
Once the image has been transferred to the remote server, ssh into the server and use the docker load command to load the transferred image.
sudo docker load < image-name.tar
Now use docker image command to make sure the image is available. Now we are ready to deploy this image using the usual docker run
command. Remember to include ports, variables within docker run
command.
docker run -d --name container_name -p 80:80 image-name
The above docker run deploy the snapshot of our docker container. Now we are almost ready to proceed further to make DNS changes to point domain name to the new server. One last thing we need to make sure is that our container does not depend on the volume. If yes, we need to manually migrate volumes from old server to the new server.
Docker persistence data is stored inside /var/lib/docker
. We need to transfer /var/lib/docker
or the specific container directory (volume) to the new server. When it’s transferred successfully to the new server, we can use docker run
command and attach the volume to container on creation.
docker run --rm --name container_name -p 80:80 -v /usr/local/bin:/target image-name
-v
parameter allows to attach the existing directory (Volume) to the container. If your old container does not require volume, then you can simply create container from the image without -v
parameter.
LinuxAndUbuntu Newsletter
Join the newsletter to receive the latest updates in your inbox.