Migrations are inevitable in many scenarios. Hardware upgrades, data center changes, obsolete OS, all these can be trigger points for migration.
In our role as Technical Support Providers for web hosting companies and infrastructure providers, we provision and manage Docker systems for their purposes.
Migrating Docker containers often forms a part of that task. Today we’ll see the different ways to move Docker container to another host from the existing 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.
Conclusion
Docker containers are widely used in DevOps and niche web hosting. Today we’ve discussed the various ways how our Docker Support Engineers move Docker container to another host in the Docker infrastructure we manage.
This is a great article. A few pointers for section 4 that helped me.
–volumes-from datavolume-name would be more clear if it were changed to –volumes-from
/path/to/volume would be more clear as /container/volume/mount/point/
-w /backup is also required so that your tarball will be created in your mounted host directory from the -v option.
This is a great article. A few pointers for section 4 that helped me.
–volumes-from datavolume-name would be more clear if it were changed to –volumes-from
/path/to/volume would be more clear as /container/volume/mount/point/
-w /backup is also required so that your tarball will be created in your mounted host directory from the -v option.
Hello,
Glad to know that article helped you.
Thank you for the tip. Edited the commands to bring in clarity.
What is the /path/to/volume or /container/volume/mount/point/ ? Could you provide an example, please? Thank you!
Hello Ralf,
We’ll be happy to talk to you on chat (click on the icon at right-bottom).