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

An example of elegant writing of judgment in JavaScript

Table of contents Preface 1. Monadic Judgment 1.1...

Node.js implements breakpoint resume

Table of contents Solution Analysis slice Resume ...

Example of customizing the style of the form file selection box

Copy code The code is as follows: <!DOCTYPE ht...

Summary of the dockerfile-maven-plugin usage guide

Table of contents pom configuration Setting.xml c...

N ways to cleverly implement adaptive dividers with CSS

Dividing lines are a common type of design on web...

Understanding of CSS selector weight (personal test)

Copy code The code is as follows: <style type=...

HTML markup language - form

Click here to return to the 123WORDPRESS.COM HTML ...

JavaScript to achieve product magnifying glass effect

This article shares the specific code of JavaScri...

Web page experience: planning and design

1. Clarify the design direction <br />First,...

Example of how to configure multiple virtual hosts in nginx

It is very convenient to configure virtual host v...

Detailed explanation of server-id example in MySQL master-slave synchronization

Preface When we build a MySQL cluster, we natural...

The difference between html block-level tags and inline tags

1. Block-level element: refers to the ability to e...

Example of javascript bubble sort

Table of contents 1. What is Bubble Sort 2. Give ...