当前位置: 代码迷 >> 综合 >> How to backup and restore docker image
  详细解决方案

How to backup and restore docker image

热度:37   发布时间:2023-12-14 23:19:28.0

在日常工作中,我们可能由于某种需要对docker镜像做备份与恢复工作,可以借助docker的两组子命令来实现,一组是docker savedocker load命令,另一组是docker exportdocker import

Save & Load

下面是一个真实的实例。

lwk@harbin:~$ docker pull xuxueli/xxl-job-admin:2.3.0
2.3.0: Pulling from xuxueli/xxl-job-admin
bf5952930446: Pull complete 
092c9b8e633f: Pull complete 
461b86d6fc62: Pull complete 
0a9dca826205: Pull complete 
efbb44ad6c82: Pull complete 
524d8fd1b10e: Pull complete 
Digest: sha256:3dc5f442c64b44c97e5b5278df259bc29a8c6b130bee903b6a004d6d00302766
Status: Downloaded newer image for xuxueli/xxl-job-admin:2.3.0
docker.io/xuxueli/xxl-job-admin:2.3.0
lwk@harbin:~$ docker images 
REPOSITORY              TAG            IMAGE ID       CREATED         SIZE
redis                   6.2.6-alpine   c08a97fdc0ef   2 weeks ago     32.3MB
ubuntu                  latest         ba6acccedd29   4 months ago    72.8MB
mysql                   8.0.23         cbe8815cbea8   11 months ago   546MB
xuxueli/xxl-job-admin   2.3.0          24cc0d9dcf54   13 months ago   223MB
mysql                   5.7.28         db39680b63ac   2 years ago     437MB
lwk@harbin:~$ docker save -o xuxuelei_xxl-job-admin_2.3.0.tar xuxueli/xxl-job-admin:2.3.0
lwk@harbin:~$ docker rmi xuxueli/xxl-job-admin:2.3.0 
Untagged: xuxueli/xxl-job-admin:2.3.0
Untagged: xuxueli/xxl-job-admin@sha256:3dc5f442c64b44c97e5b5278df259bc29a8c6b130bee903b6a004d6d00302766
Deleted: sha256:24cc0d9dcf54471b1bb2b63610ffaead977409b784c0fdeab7712d86b3b77663
Deleted: sha256:58dba8f5d6f6f9d308c023c44388812b6d7c6a35aecd2b0746c0a5ebc7a5d8fb
Deleted: sha256:35707f6d3f36d0f49601eecf51d2214d1f5d7165b6c962d61d6ecdb53535f62b
Deleted: sha256:80126c28adf3fa9753970f63ca234d93784dbb7ef8e434c88d4810c7b1372fe6
Deleted: sha256:216b95f0ebd672f3a7e0fd747aa175ba72bf0f8f34652e004188d08b469aac84
Deleted: sha256:f33f8fd6018d7919e0ac50b1f44c9b99dc89229c31c6969c473ed8320959bd59
Deleted: sha256:d0f104dc0a1f9c744b65b23b3fd4d4d3236b4656e67f776fe13f8ad8423b955c
lwk@harbin:~$ docker images 
REPOSITORY   TAG            IMAGE ID       CREATED         SIZE
redis        6.2.6-alpine   c08a97fdc0ef   2 weeks ago     32.3MB
ubuntu       latest         ba6acccedd29   4 months ago    72.8MB
mysql        8.0.23         cbe8815cbea8   11 months ago   546MB
mysql        5.7.28         db39680b63ac   2 years ago     437MB
lwk@harbin:~$ docker load < xuxuelei_xxl-job-admin_2.3.0.tar 
d0f104dc0a1f: Loading layer [==================================================>]  72.49MB/72.49MB
95c20fa5728d: Loading layer [==================================================>]  9.079MB/9.079MB
ed7152ed4cbd: Loading layer [==================================================>]  3.584kB/3.584kB
43ee8102614d: Loading layer [==================================================>]  106.2MB/106.2MB
c39ce324e48b: Loading layer [==================================================>]  3.072kB/3.072kB
13562a79b4ca: Loading layer [==================================================>]     39MB/39MB
Loaded image: xuxueli/xxl-job-admin:2.3.0
lwk@harbin:~$ docker images 
REPOSITORY              TAG            IMAGE ID       CREATED         SIZE
redis                   6.2.6-alpine   c08a97fdc0ef   2 weeks ago     32.3MB
ubuntu                  latest         ba6acccedd29   4 months ago    72.8MB
mysql                   8.0.23         cbe8815cbea8   11 months ago   546MB
xuxueli/xxl-job-admin   2.3.0          24cc0d9dcf54   13 months ago   223MB
mysql                   5.7.28         db39680b63ac   2 years ago     437MB
lwk@harbin:~$

Export & Import

lwk@harbin:~$ docker images 
REPOSITORY              TAG            IMAGE ID       CREATED         SIZE
redis                   6.2.6-alpine   c08a97fdc0ef   2 weeks ago     32.3MB
ubuntu                  latest         ba6acccedd29   4 months ago    72.8MB
mysql                   8.0.23         cbe8815cbea8   11 months ago   546MB
xuxueli/xxl-job-admin   2.3.0          24cc0d9dcf54   13 months ago   223MB
mysql                   5.7.28         db39680b63ac   2 years ago     437MB
lwk@harbin:~$
lwk@harbin:~$ docker ps -a
CONTAINER ID   IMAGE                COMMAND                  CREATED       STATUS          PORTS                                       NAMES
45e0caa8dcfc   redis:6.2.6-alpine   "redis-server --appe…"   2 weeks ago   Up 53 minutes   0.0.0.0:6379->6379/tcp, :::6379->6379/tcp   redis
lwk@harbin:~$ docker export redis > redis_6.2.6-alpine.tar
lwk@harbin:~$ ll
total 32908
-rw-rw-r--  1 lwk  lwk  33403904 Mar 10 10:25 redis_6.2.6-alpine.tar
lwk@harbin:~$
lwk@harbin:~$ docker stop redis 
redis
lwk@harbin:~$ docker ps -a
CONTAINER ID   IMAGE                COMMAND                  CREATED       STATUS                      PORTS     NAMES
45e0caa8dcfc   redis:6.2.6-alpine   "redis-server --appe…"   2 weeks ago   Exited (0) 14 seconds ago             redis
lwk@harbin:~$ docker rm redis 
redis
lwk@harbin:~$ docker images 
REPOSITORY              TAG            IMAGE ID       CREATED         SIZE
redis                   6.2.6-alpine   c08a97fdc0ef   2 weeks ago     32.3MB
ubuntu                  latest         ba6acccedd29   4 months ago    72.8MB
mysql                   8.0.23         cbe8815cbea8   11 months ago   546MB
xuxueli/xxl-job-admin   2.3.0          24cc0d9dcf54   13 months ago   223MB
mysql                   5.7.28         db39680b63ac   2 years ago     437MB
lwk@harbin:~$ docker rmi redis:6.2.6-alpine 
Untagged: redis:6.2.6-alpine
Deleted: sha256:c08a97fdc0efe82b93f8ae596790b65f02aff40914bdf5f93a73356788be7ca7
Deleted: sha256:17dee2fdce28c6bd91878ddb15db16513cbc4a59a7b1fad659d83ff872de6054
lwk@harbin:~$ docker images 
REPOSITORY              TAG       IMAGE ID       CREATED         SIZE
ubuntu                  latest    ba6acccedd29   4 months ago    72.8MB
mysql                   8.0.23    cbe8815cbea8   11 months ago   546MB
xuxueli/xxl-job-admin   2.3.0     24cc0d9dcf54   13 months ago   223MB
mysql                   5.7.28    db39680b63ac   2 years ago     437MB
lwk@harbin:~$ 
lwk@harbin:~$ docker import - redis:6.2.6-alpine < redis_6.2.6-alpine.tar 
sha256:eb8c1667848fad5ac5ccd254a09963c5129e849afd9d42e8b73c3075429fde6d
lwk@harbin:~$ docker images 
REPOSITORY              TAG            IMAGE ID       CREATED         SIZE
redis                   6.2.6-alpine   eb8c1667848f   7 seconds ago   32.3MB
ubuntu                  latest         ba6acccedd29   4 months ago    72.8MB
mysql                   8.0.23         cbe8815cbea8   11 months ago   546MB
xuxueli/xxl-job-admin   2.3.0          24cc0d9dcf54   13 months ago   223MB
mysql                   5.7.28         db39680b63ac   2 years ago     437MB
lwk@harbin:~$

小结

如果使用 import 导入 save 产生的文件,虽然导入不提示错误,但是启动容器时会提示失败,会出现类似"docker: Error response from daemon: Container command not found or does not exist"的错误。

  • 文件大小不同

export 导出的镜像文件体积小于 save 保存的镜像

  • 是否可以对镜像重命名

docker import 可以为镜像指定新名称
docker load 不能对载入的镜像重命名

  • 是否可以同时将多个镜像打包到一个文件中

docker export 不支持
docker save 支持

  • 是否包含镜像历史

export 导出(import 导入)是根据容器拿到的镜像,再导入时会丢失镜像所有的历史记录和元数据信息(即仅保存容器当时的快照状态),所以无法进行回滚操作。
而 save 保存(load 加载)的镜像,没有丢失镜像的历史,可以回滚到之前的层(layer)。

  • 应用场景不同

docker export 的应用场景:主要用来制作基础镜像,比如我们从一个 ubuntu 镜像启动一个容器,然后安装一些软件和进行一些设置后,使用 docker export 保存为一个基础镜像。然后,把这个镜像分发给其他人使用,比如作为基础的开发环境。
docker save 的应用场景:如果我们的应用是使用 docker-compose.yml 编排的多个镜像组合,但我们要部署的客户服务器并不能连外网。这时就可以使用 docker save 将用到的镜像打个包,然后拷贝到客户服务器上使用 docker load 载入。

Reference

  • Docker - 实现本地镜像的导出、导入
  相关解决方案