Explanation of 5 Methods of Migrating Docker Container to Other Servers

  • 2021-10-25 00:08:49
  • OfStack

Migration is inevitable in many cases. Hardware upgrades, data center changes, and outdated operating systems can all be trigger points for migration.

Docker container migration is usually part 1 of the migration task. Today we will see different ways to migrate the Docker container from an existing server to another server.

How to migrate the Docker container to another server. There is no way to migrate the Docker container directly from one server to another. We solve the problem of Docker container migration by using one or more of the following methods.

1. Export and Import Containers

Exporting a container means creating a compressed file from the container's file system and saving the exported file as an "gzip" file.


docker export container-name | gzip > container-name.gz

The compressed file is then copied to the new server through a file transfer tool, such as scp or rsync. On the new server, this gzip file is then imported into a new container.


zcat container-name.gz | docker import - container-name

You can access the new container created in the new server using the "docker run" command.

One disadvantage of the Export Container tool is that it does not export the ports and variables of the container, nor does it export the underlying data that contains the container.

This may cause an error when trying to load the container in another server. In this case, we chose Docker Mirror Migration to migrate the container from one server to another.

2. Container mirroring migration

The most common way to migrate an Docker container to another server is to migrate the images that the container is associated with.

For containers that must be migrated, first use the "Docker commit" command to save their Docker image to a zip file.


docker commit container-id image-name

The resulting image will be compressed and uploaded to the new server, where a new container will be created using "docker run".

With this method, the data volume is not migrated, but it retains the data of the application created in the container.

3. Save and Load the Image

An docker image is a package of code, libraries, configuration files, and so on for an application. The Docker container is created by these images.

You can use "docker save" to compress the image and migrate it to a new server.


docker save image-name > image-name.tar

On the new server, use "docker load" to use the compressed image file to create a new image.


cat image-name.tar | docker load

4. Migrating data volumes

Data volumes in an Docker container are shared directories that contain container-specific data. The data in the volume is persistent and will not be lost during container recreation.

When you migrate an Docker container or image from one server to another using the export or submit tool, the underlying data volume is not migrated.

In this case, the directory containing the data will be manually migrated to the new server. Then create a container on the new server and reference the directory as its data volume.

Another simple method is to back up and restore the data volume by passing the "-volumes from" parameter in the "docker run" command.


docker run --rm --volumes-from datavolume-name -v $(pwd):/backup image-name tar cvf backup.tar /path-to-datavolume 

Here, the name of datavolume is/path/to/volume. This command provides a backup of the data volume. To specify a working directory, you can also specify-w/backup. Backups generated in the/backup folder can be copied to the new server using the scp or ftp tools. Then extract the copied backup and restore it to the data volume in the new container.


docker run --rm --volumes-from datavolume-name -v $(pwd):/backup image-name bash -c "cd /path-to-datavolume && tar xvf /backup/backup.tar --strip 1"

5. Migrate the entire Docker container

The method we see here applies to a single container. However, in the case of migrating all containers from one server to another, we use another method.

This method involves copying the entire docker directory ("/var/lib/docker") to the new server. In order for this method to succeed, several key points need to be identified.

Preserve permissions and ownership of the folder. Stop the Docker service before migration. Verify that the Docker versions on both servers are compatible. Verify container lists and features before and after migration. Path to environment variables and other configuration files.

If this method does not work due to any failure, we will configure custom scripts to migrate containers and images from one server to another.

Conclusion: Docker containers are widely used in DevOps and web-based hosting. Today we discussed various ways for Docker engineers to migrate the Docker container to another server in the Docker infrastructure we manage.


Related articles: