Migrating Docker Images, Containers, and Volumes: Practical Techniques
This article explains how to migrate Docker images, containers, and data volumes using save/load, export/import, and backup/restore commands, offering practical steps for offline environments, complex production services, and volume handling while highlighting the limitations of conventional approaches.
Even deployed containerized services require maintenance, and production environments often demand unconventional operations such as migrating images, containers, and volumes.
Image
Image migration is suitable for offline environments where a Docker registry may not be available.
Save
# use stdout
docker save alpine > /tmp/alpine.tar
# or write to a file directly
docker save alpine -o /tmp/alpine.tarUsing the -o option is recommended because stdout may not work in some scenarios, such as remote SSH execution.
Load
# use stdout
docker load < /tmp/wekan.tar
# or read from a file directly
docker load -i /tmp/wekan.tarContainer
Container migration applies to services already online with complex state that cannot be restarted from scratch; the migration package includes the image.
Export
$ docker run --rm -d --name test alpine tail -f /dev/null
9232f0c1dafe0f29918f281ca37bb41914677e818cb6f252abf3dab3be04fbb2
$ docker exec test touch proof
$ docker exec test ls -hl proof
-rw-r--r-- 1 root root 0 Nov 20 14:33 proof
#
执行导出操作:
docker export test -o test.tarImport
$ docker kill test
test
#
执行导入操作:
$ docker import test.tar test-img
sha256:e03727eeba7e16dd3acfcc7536f1244762508f9b6b9856e49cc837c1b7ffa444Note that import creates an image, similar to a docker commit , which is not a recommended operation.
Verify the imported file:
$ docker run --rm -d --name test test-img tail -f /dev/null
ee29cb63bb2d3ed8ac890789ba80c4fe4078b9d5343a8952b6217d64b4dcbe23
$ docker exec test ls -hl proof
-rw-r--r-- 1 root root 0 Nov 20 14:33 proofVolume
Volume migration is more cumbersome because Docker does not provide an official simple solution.
Directly accessing Docker's data directory as root is possible but considered the worst approach.
The recommended method uses another container to package the volume content and transfer it via a mounted host directory.
Backup
First, create a volume:
$ docker run --rm -d --name test -v test-vol:/data test-img tail -f /dev/null
f4ff81f4c31025ff476fbebc2c779a915b43ba5940b5bcc42e3ef9b1379eaeab
$ docker exec test touch /data/proof
$ docker exec test ls -hl proof
-rw-r--r-- 1 root root 0 Nov 20 14:40 proofExecute backup:
$ docker run --rm -v test-vol:/volume -v $PWD:/backup alpine tar cvf /backup/backup.tar volume
volume/
volume/proofCopying the tar file with docker cp is another option but may affect running containers.
Restore
First, clean up the container and volume:
$ docker kill test
test
$ docker volume rm test-vol
test-volExecute restore:
docker run --rm -v test-vol:/volume -v $PWD:/backup alpine tar xf /backup/backup.tarCheck the restored result:
$ docker run --rm -v test-vol:/data alpine ls -ahl /data
total 8
drwxr-xr-x 2 root root 4.0K Nov 20 14:48 .
drwxr-xr-x 1 root root 4.0K Nov 20 14:50 ..
-rw-r--r-- 1 root root 0 Nov 20 14:40 proofConclusion
These three sets of techniques are unconventional; image transfer should rely on an internal Docker registry rather than tar files, containers should be stateless and restartable, and volume migration requires dedicated tools rather than manual copying.
Architecture Digest
Focusing on Java backend development, covering application architecture from top-tier internet companies (high availability, high performance, high stability), big data, machine learning, Java architecture, and other popular fields.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.