Migrate Docker container to new server

Docker Docker

There are many ways of migrating Docker containers to a different server, today I’ll show you one of the possible approaches.

IMPORTANT: it’s a beginner’s tutorial for copying basic Docker containers (no external dependencies like additional networks or storage volumes).

If you have filesystem volumes attached to your original Docker container, this procedure will not be enough. I’ll publish a more advanced tutorial soon – stay tuned.

This is a simple enough procedure. Steps 1, 2 and 3 should be done on the old server, Steps 4, 5 and 6 should be done on the new server. All you need is root access on both servers and a way to transfer images between the two servers (scp, for instance).

Step 1: Stop Docker container

I’m hoping to transfer the database container called db (container id c745794419a9 below):

root@oldserver:/ # docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1b8b1657736e datadog/agent:latest "/init" 9 months ago Up 26 hours (healthy) 8125/udp, 8126/tcp dd-agent
c745794419a9 mariadb:latest "docker-entrypoint.s…" 9 months ago Up 29 minutes 3306/tcp db
32cd3e477546 nginx:latest "nginx -g 'daemon of…" 12 months ago Up 26 hours 0.0.0.0:80->80/tcp, 0.0.0.0:443->443/tcp nginx

Let’s stop the container:

root@oldserver:/ # docker stop db
db

…then make sure it’s down:

root@oldserver:/ # docker ps --all | grep c745794419a9
c745794419a9 mariadb:latest "docker-entrypoint.s…" 9 months ago Exited (0) About an hour ago db

Step 2. Commit Docker container to image

root@oldserver:/ # docker commit c745794419a9
sha256:9d07849ed7c73f8fecd1e5e3e2aedc3592eea6b02f239fa6efba903f1a1ef835

Step 3: Save Docker image to a file

root@oldserver:/ # docker save 9d07849ed7c73f8fecd1e5e3e2aedc3592eea6b02f239fa6efba903f1a1ef835 > s5-db.tar

Step 4: Transfer Docker image file

Step 5: Load Docker image from a file

On the new server, we docker load the image. Note how it is the same image ID:

root@newserver:/ # cat s5-db.tar | docker load
4bcdffd70da2: Loading layer [==================================================>] 129.3MB/129.3MB
ae12d30e1dfc: Loading layer [==================================================>] 345.1kB/345.1kB
7a065b613dee: Loading layer [==================================================>] 3.178MB/3.178MB
cb2872ddbc2c: Loading layer [==================================================>] 1.536kB/1.536kB
328a5e02ea3f: Loading layer [==================================================>] 15.05MB/15.05MB
736f4a72442b: Loading layer [==================================================>] 25.6kB/25.6kB
3fbb3db5b99e: Loading layer [==================================================>] 5.12kB/5.12kB
fbf207c08d17: Loading layer [==================================================>] 5.12kB/5.12kB
c61ded92b25c: Loading layer [==================================================>] 257MB/257MB
74569dcf2238: Loading layer [==================================================>] 8.704kB/8.704kB
b954e0840314: Loading layer [==================================================>] 1.536kB/1.536kB
9b819b273348: Loading layer [==================================================>] 2.56kB/2.56kB
Loaded image ID: sha256:9d07849ed7c73f8fecd1e5e3e2aedc3592eea6b02f239fa6efba903f1a1ef835

Step 6: Start Docker container

Now let’s start a Docker container from this image:

root@newserver:/ # docker run -d --name db-new 9d07849ed7c73f8fecd1e5e3e2aedc3592eea6b02f239fa6efba903f1a1ef835
1ca6041d6e1e6c661234e24b16c0d23b0a302586f8628809020d5469e3acd405

As you can see, it’s running now:

root@newserver:/ # docker ps | grep db-new
1ca6041d6e1e 9d07849ed7c7 "docker-entrypoint..." 5 seconds ago Up 3 second

See Also




Contact Me

Follow me on Facebook, Twitter or Telegram:
Recommended
I learn with Educative: Educative
IT Consultancy
I'm a principal consultant with Tech Stack Solutions. I help with cloud architectrure, AWS deployments and automated management of Unix/Linux infrastructure. Get in touch!
Recent Articles
03 Sep 2024

macOS Sequoia 15.0

02 Sep 2024

Video: Has This Worked Before?

24 Mar 2024

Homelab: Mac Pro 2013 32GB

Recent Tweets