Docker Export and Migration

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

1 docker save

docker save saves all layers of a Docker image into a tar file, with the command as follows:

1
2
docker save IMAGE -o 114.tar
docker save IMAGE > 114.tar # Redirecting can also save

The corresponding read command for docker save is docker load.

2 docker export

docker export saves the current state of a Docker container (whether the container is running or stopped) into a tar file, and saves it as a flattened layer, with the command as follows:

1
2
docker export CONTAINER -o 114.tar
docker export CONTAINER > 114.tar # Redirecting can also save

The corresponding command for docker export is docker import.

3 Differences between docker save and docker export

The tar file output by docker save contains all the layers, configurations, and metadata of the image, retaining all build information and version history of the image. The output of 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 publishing applications. If there is a need for continuous modification and development of container content, it is better to use docker commit + docker save, which allows rollback like git.

4 Actual 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

Export the container image using docker export:

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

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

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 modifications made in another container are 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

You can see this is not the previous container, but the modifications are retained.

For a single container, this method can be used for quick import and export, but if it is a group of containers created by docker compose, load and save need to be used. The key difference between load and import, besides the different usage scenarios, is that export does not save the container’s version history, and can be considered as a snapshot at a point in time. For more detailed content, you can refer to this article.

Besides transferring containers using files, you can also use Docker Hub and Dockerfile, etc.

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