5 ways to migrate Docker containers to other servers

5 ways to migrate Docker containers to other servers

Migration is unavoidable in many cases. Hardware upgrades, data center changes, outdated operating systems, all of these can be triggers for migration.

Docker container migration is often part of a migration task. Today we will see different ways to migrate Docker containers from an existing server to another server.

How to migrate Docker container to another server. There is no direct way to migrate Docker container 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. The exported file is saved as a "gzip" file.

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

Then copy the compressed file to the new server via a file transfer tool such as scp or rsync. In the new server, this gzip file is then imported into a new container.

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

The new container created in the new server can be accessed using the "docker run" command.

One drawback of the export container tool is that it does not export the container's ports and variables, nor does it export the underlying data that contains the container.

This may cause errors when trying to load the container in another server. In this case, we choose Docker image migration to migrate the container from one server to another.

2. Container image migration

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

For the container that must be migrated, first save its Docker image into a compressed file using the "Docker commit" command.

docker commit container-id image-name

The resulting image will be zipped 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 preserves the data of the application created inside the container.

3. Save and load images

A Docker image is a package of your application's code, libraries, configuration files, etc. Docker containers are created from these images.

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

docker save image-name > image-name.tar

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

cat image-name.tar | docker load

4. Migrate data volumes

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

When you migrate a Docker container or image from one server to another using the export or commit tools, the underlying data volumes are not migrated.

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

Another simple way is to backup and restore data volumes 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 datavolume name is /path/to/volume. This command provides a backup of the data volume. To specify a working directory, you can also specify -w/backup. The backup generated in the /backup folder can be copied to the new server via scp or ftp tools. The copied backup is then extracted and restored 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 approach we've seen here works for a single container. But in the case where all containers need to be migrated from one server to another, we take another approach.

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

  • Preserve folder permissions and ownership.
  • Stop the Docker service before migration.
  • Verify that the Docker versions in both servers are compatible.
  • Verify container list and functionality before and after migration.
  • Paths to environment variables and other configuration files.

If this method does not work due to any failure, we 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 that Docker engineers can migrate Docker containers to another server in the Docker infrastructure we manage.

This concludes the article on 5 ways to migrate Docker containers to other servers. For more information about migrating Docker containers to servers, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Docker time zone issue and data migration issue
  • Docker image import, export, backup and migration operations
  • Detailed explanation of migrating local Docker containers to the server
  • Migrating the blog to Docker
  • How to migrate docker containers across servers
  • How to migrate the data directory in Docker

<<:  Several ways for Vue to achieve communication between components (multiple scenarios)

>>:  Free tool to verify that HTML, CSS and RSS feeds are correct

Recommend

How to set PATH environment variable in Linux system (3 methods)

1. In Windows system, many software installations...

js realizes the magnifying glass function of shopping website

This article shares the specific code of js to re...

Node.js+express+socket realizes online real-time multi-person chat room

This article shares the specific code of Node.js+...

Web design tips on form input boxes

1. Dashed box when cancel button is pressed <br...

Analysis of the principle of Rabbitmq heartbea heartbeat detection mechanism

Preface When using RabbitMQ, if there is no traff...

JavaScript canvas implements graphics and text with shadows

Use canvas to create graphics and text with shado...

js to achieve a simple carousel effect

This article shares the specific code of js to ac...

Sample code for automatic web page refresh and automatic jump

Automatic web page refresh: Add the following code...

Web lesson plans, lesson plans for beginners

Teaching Topics Web page Applicable grade Second ...

InnoDB type MySql restore table structure and data

Prerequisite: Save the .frm and .ibd files that n...

VMware Workstation virtual machine installation operation method

Virtual machines are very convenient testing soft...

Analysis of the Linux input subsystem framework principle

Input subsystem framework The linux input subsyst...

Example of how to quickly delete a 2T table in mysql in Innodb

Preface This article mainly introduces the releva...