Docker Export and Migration

This article mainly introduces the operation methods of Docker migration and backup, as well as the difference between docker save and docker export.

docker save

docker save saves all layers of a Docker image to a tar file, as shown in the following commands:

1
2
docker save IMAGE -o 114.tar
docker save IMAGE > 114.tar # Saving can also be done using redirection

The corresponding command to read with docker save is docker load.

docker export

docker export saves the current state of a Docker container (whether the container is running or stopped) to a tar file, and saves as a flattened layer, as shown in the following commands:

1
2
docker export CONTAINER -o 114.tar
docker export CONTAINER > 114.tar # Saving can also be done using redirection

The corresponding command for docker export is docker import.

Differences between docker save and docker export

The tar file output by docker save contains all layers, configuration, and metadata of the image, retaining all the build information and version history. The output by docker export is a flattened file system, containing only the current state of the container, without any build or layer information. Generally, the result of export is smaller in size, suitable for releasing applications. If there is a continuous modification and development need on the container content, it’s better to use docker commit + docker save, which allows rollback like git.

Practical Test

First, make some modifications in a container, write a text file in the root directory:

1
2
3
4
5
6
root@minedl:~# docker run -it ubuntu:latest
root@ba2179ff43aa:/# ls
bin  boot  dev  etc  home  lib  lib32  lib64  libx32  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
root@ba2179ff43aa:/# echo 1111 >> 1.txt
root@ba2179ff43aa:/# cat 1.txt
1111

Use docker export to export the container’s image:

1
2
3
root@minedl:~# docker export ba2179ff43aa > me.tar
root@minedl:~# ls
me.tar  snap

Import the image using docker import, you can see the imported image named me:

1
2
3
4
5
6
root@minedl:~# docker import - me <  me.tar
sha256:8f5e159f2e21451ff2341526572bc3c67e9240454df0341a2ce447586b9e29a1
root@minedl:~# docker images
REPOSITORY    TAG       IMAGE ID       CREATED         SIZE
me            latest    8f5e159f2e21   4 seconds ago   77.8MB
mydoc         latest    40dd787fd6e9   9 minutes ago   77.8MB

Create a container using the imported me image and check if the previous modification in another container is retained:

1
2
3
4
5
root@minedl:~# docker run -it me /bin/bash
root@3f21d7037f50:/# ls
1.txt  bin  boot  dev  etc  home  lib  lib32  lib64  libx32  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
root@3f21d7037f50:/# cat 1.txt
1111

It can be seen that this is not the previous container, but the modification is retained.

For individual containers, this method can be used for quick import and export, but for container groups created by docker compose, load and save need to be used. Besides the different application scenarios, the key point is that export does not save container version history, which can be considered a snapshot of a point in time. More detailed content can be referred to this article.

Besides transferring containers via files, docker hub and docker files, etc., can also be used.

Buy me a coffee~
Tim AlipayAlipay
Tim PayPalPayPal
Tim WeChat PayWeChat Pay
0%