Sunday 26 February 2023

5 ways to move Docker container to another host

 

How to move Docker container to another host

There is no straightforward way to directly move Docker container from one host to another. We workaround this by using one or more of these methods for the migration.

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

 

This compressed file is then copied over to the new host via file transfer tools such as scp or rsync. In the new host, 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 host can be accessed using ‘docker run’ command.

One drawback of export tool is that, it does not copy ports and variables, or the underlying data volume which contains the container data.

This can lead to errors when trying to load the container in another host. In such cases, we opt for Docker image migration to move containers from one host to another.

 

2. Container image migration

The most commonly used method to move Docker container to another host, is by migrating the image linked to that container.

For the container that has to be moved, first its Docker image is saved into a compressed file using ‘docker commit’ command.

docker commit container-id image-name

 

The image that is generated is compressed and moved into the new host machine. In the new host, a new container is created with ‘docker run’.

Using this method, the data volumes will not be 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 code, libraries, configuration files, etc. for an application. Docker containers are created out of these images.

The images can be compressed using ‘docker save’ and moved to a new host.

docker save image-name > image-name.tar

 

In the new host, this compressed image file can be used to create new image using ‘docker load’.

cat image-name.tar | docker load

 

4. Migrate data volumes

Data volumes in Docker machines are shared directories that contains the data specific to containers. The data in volumes are persistent and will not be lost during container recreation.

When Docker containers or images are moved from one host to another using export or commit tools, the underlying data volume is not migrated.

In such situations, the directory containing data is manually moved to the new host. Then containers are created there with reference to that directory as its data volume.

Another fool proof method is to backup and restore the data volume by passing ‘–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, datavolume-name is the /path/to/volume. This command provides a backup of the data volume. To specify the working directory, we can specify the -w /backup as well. The backup generated in /backup folder can be moved to new host via scp or ftp tools.

Copied backup is then extracted and restored to the data volume in the new container there.

 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. Move entire Docker containers

The methods we saw here are applicable for individual containers. But in cases where all the containers are to be moved from one host to another, we adopt another method.

This method includes copying the entire “/var/lib/docker” directory to new host. To make this method successful, a few critical points are ensured.

  • The permissions and ownership of the folders are preserved.
  • Docker service is stopped before the move.
  • Docker versions in two hosts are verified to be compatible.
  • Container list and functionality is verified before and after the move.
  • Paths to the entry points and other configuration files are maintained.

In cases when this method does not work due to any hiccups, we configure custom scripts to migrate the containers and images from one host to another.

No comments:

Post a Comment