Bobcares

5 ways to move Docker container to another host

by | Nov 7, 2018

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.

 

PREVENT YOUR SERVER FROM CRASHING!

Never again lose customers to poor server speed! Let us help you.

Our server experts will monitor & maintain your server 24/7 so that it remains lightning fast and secure.

SEE SERVER ADMIN PLANS

var google_conversion_label = "owonCMyG5nEQ0aD71QM";

5 Comments

  1. WrinkledCheese

    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.

    Reply
  2. email

    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.

    Reply
    • Sijin George

      Hello,
      Glad to know that article helped you.
      Thank you for the tip. Edited the commands to bring in clarity.

      Reply
  3. Ralf

    What is the /path/to/volume or /container/volume/mount/point/ ? Could you provide an example, please? Thank you!

    Reply
    • Maheen Aboobakkar

      Hello Ralf,

      We’ll be happy to talk to you on chat (click on the icon at right-bottom).

      Reply

Submit a Comment

Your email address will not be published. Required fields are marked *

Never again lose customers to poor
server speed! Let us help you.

Privacy Preference Center

Necessary

Necessary cookies help make a website usable by enabling basic functions like page navigation and access to secure areas of the website. The website cannot function properly without these cookies.

PHPSESSID - Preserves user session state across page requests.

gdpr[consent_types] - Used to store user consents.

gdpr[allowed_cookies] - Used to store user allowed cookies.

PHPSESSID, gdpr[consent_types], gdpr[allowed_cookies]
PHPSESSID
WHMCSpKDlPzh2chML

Statistics

Statistic cookies help website owners to understand how visitors interact with websites by collecting and reporting information anonymously.

_ga - Preserves user session state across page requests.

_gat - Used by Google Analytics to throttle request rate

_gid - Registers a unique ID that is used to generate statistical data on how you use the website.

smartlookCookie - Used to collect user device and location information of the site visitors to improve the websites User Experience.

_ga, _gat, _gid
_ga, _gat, _gid
smartlookCookie
_clck, _clsk, CLID, ANONCHK, MR, MUID, SM

Marketing

Marketing cookies are used to track visitors across websites. The intention is to display ads that are relevant and engaging for the individual user and thereby more valuable for publishers and third party advertisers.

IDE - Used by Google DoubleClick to register and report the website user's actions after viewing or clicking one of the advertiser's ads with the purpose of measuring the efficacy of an ad and to present targeted ads to the user.

test_cookie - Used to check if the user's browser supports cookies.

1P_JAR - Google cookie. These cookies are used to collect website statistics and track conversion rates.

NID - Registers a unique ID that identifies a returning user's device. The ID is used for serving ads that are most relevant to the user.

DV - Google ad personalisation

_reb2bgeo - The visitor's geographical location

_reb2bloaded - Whether or not the script loaded for the visitor

_reb2bref - The referring URL for the visit

_reb2bsessionID - The visitor's RB2B session ID

_reb2buid - The visitor's RB2B user ID

IDE, test_cookie, 1P_JAR, NID, DV, NID
IDE, test_cookie
1P_JAR, NID, DV
NID
hblid
_reb2bgeo, _reb2bloaded, _reb2bref, _reb2bsessionID, _reb2buid

Security

These are essential site cookies, used by the google reCAPTCHA. These cookies use an unique identifier to verify if a visitor is human or a bot.

SID, APISID, HSID, NID, PREF
SID, APISID, HSID, NID, PREF