Docker ❀ save load export import 镜像与容器的导入与导出

文章目录

Docker相关命令常用参数


[root@localhost ~]# docker --
--add-host                     --cpu-shares                   --env-file                     --kernel-memory                --no-healthcheck               --stop-signal
--attach                       --detach                       --expose                       --label                        --oom-kill-disable             --stop-timeout
--blkio-weight                 --detach-keys                  --gpus                         --label-file                   --oom-score-adj                --storage-opt
--blkio-weight-device          --device                       --group-add                    --link                         --pid                          --sysctl
--cap-add                      --device-cgroup-rule           --health-cmd                   --link-local-ip                --pids-limit                   --tmpfs
--cap-drop                     --device-read-bps              --health-interval              --log-driver                   --privileged                   --tty
--cgroupns                     --device-read-iops             --health-retries               --log-opt                      --publish                      --ulimit
--cgroup-parent                --device-write-bps             --health-start-period          --mac-address                  --publish-all                  --user
--cidfile                      --device-write-iops            --health-timeout               --memory                       --pull                         --userns
--cpu-period                   --disable-content-trust=false  --help                         --memory-reservation           --read-only                    --uts
--cpu-quota                    --dns                          --hostname                     --memory-swap                  --restart                      --volume
--cpu-rt-period                --dns-option                   --init                         --memory-swappiness            --rm                           --volume-driver
--cpu-rt-runtime               --dns-search                   --interactive                  --mount                        --runtime                      --volumes-from
--cpus                         --domainname                   --ip                           --name                         --security-opt                 --workdir
--cpuset-cpus                  --entrypoint                   --ip6                          --network                      --shm-size                     
--cpuset-mems                  --env                          --ipc                          --network-alias                --sig-proxy=false   

1、构建镜像


为了完成save与load,需要首先构建一个新的镜像,区别其他镜像;

#爬取centos最新版本镜像文件,若已经本地存在,则忽略此步骤
[root@localhost ~]# docker pull centos
[root@localhost ~]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED         SIZE
centos        latest    5d0da3dc9764   2 months ago    231MB

#使用DockerFile构建新镜像文件,区分与新爬取的镜像
[root@localhost ~]# cat /tmp/docker_test 
FROM centos
RUN yum install -y vim
RUN yum install -y net-tools
CMD /bin/bash

#创建新镜像,镜像版本自定义
[root@localhost ~]# docker build -f /tmp/docker_test -t mycentos:1.1 .
Sending build context to Docker daemon  10.67MB
Step 1/4 : FROM centos
 ---> 5d0da3dc9764
Step 2/4 : RUN yum install -y vim
 ---> Running in fd8ea201e781
Complete!
Removing intermediate container fd8ea201e781
 ---> 705f34782fe8
Step 3/4 : RUN yum install -y net-tools
 ---> Running in 2b55f92b2400
Transaction Summary
Complete!
Removing intermediate container 2b55f92b2400
 ---> 3cf9004e05db
Step 4/4 : CMD /bin/bash
 ---> Running in 6f5051dd5df6
Removing intermediate container 6f5051dd5df6
 ---> a57146ea148c
Successfully built a57146ea148c
Successfully tagged mycentos:1.1

#查看自定义镜像是否与源镜像存在区别
[root@localhost ~]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED         SIZE
mycentos      1.1       a57146ea148c   6 seconds ago   322MB
centos        latest    5d0da3dc9764   2 months ago    231MB

2、docker save&load


#查看save帮助信息
[root@localhost ~]# docker save --help

Usage:  docker save [OPTIONS] IMAGE [IMAGE...]

Save one or more images to a tar archive (streamed to STDOUT by default)

Options:
  -o, --output string   Write to a file, instead of STDOUT

#保存新镜像为压缩文件(镜像保存的方式为tar文件)
[root@localhost ~]# docker save mycentos:1.1 -o /tmp/docker_test.tar

#查看保存后的文件
[root@localhost ~]# ll -dh /tmp/docker_test.tar 
-rw-------. 1 root root 316M Nov 25 23:45 /tmp/docker_test.tar

#为了区分导入效果,删除已经存在的新镜像
[root@localhost ~]# docker rmi mycentos:1.1 
Untagged: mycentos:1.1
Deleted: sha256:a57146ea148c22072e0d38f0a54f1f6980a41a635569e0bd0cbe0ca20a16056a
Deleted: sha256:3cf9004e05db213e83d5d2a07437aefdc027ea5a6231975442c53965697e81e8
Deleted: sha256:567a09c801ae815e0f77d51d5a53b199d63dff41a7018507403cb20a7c1719c2
Deleted: sha256:705f34782fe83875c7b93ff02b355a3d1a0f1254e1999c4473e1060be1c45a8e
Deleted: sha256:ea725633ca1e7902c20ad8f8955108e2ce1159f3a74be4cc3918206a5f5fb3d4

#查看load帮助信息
[root@localhost ~]# docker load --help

Usage:  docker load [OPTIONS]

Load an image from a tar archive or STDIN

Options:
  -i, --input string   Read from tar archive file, instead of STDIN
  -q, --quiet          Suppress the load output

#使用load导入相关tar文件
[root@localhost ~]# docker load -i /tmp/docker_test.tar 
df73d6b025a6: Loading layer [==================================================>]  65.34MB/65.34MB
53aeac00a22a: Loading layer [==================================================>]  27.04MB/27.04MB
Loaded image: mycentos:1.1

#查看导入结果
[root@localhost ~]# docker images | grep centos
mycentos      1.1       a57146ea148c   8 minutes ago   322MB
centos        latest    5d0da3dc9764   2 months ago    231MB

3、docker export&import


#查看export帮助信息
[root@localhost ~]# docker export --help

Usage:  docker export [OPTIONS] CONTAINER

Export a container's filesystem as a tar archive

Options:
  -o, --output string   Write to a file, instead of STDOUT

#使用新镜像创建新容器
[root@localhost ~]# docker run -it --name docker_test_001 -d mycentos:1.1
4f0bddcc627d85e1d1e94ae4b7fb42afcc5cb937f8760ff10b53887122503366

#查看新创建的容器信息
[root@localhost ~]# docker ps 
CONTAINER ID   IMAGE          COMMAND                  CREATED         STATUS         PORTS     NAMES
4f0bddcc627d   mycentos:1.1   "/bin/sh -c /bin/bash"   3 seconds ago   Up 2 seconds             docker_test_001

#使用export导出新创建的容器
[root@localhost ~]# docker export docker_test_001 -o /tmp/docker_test2.tar

#查看导出文件
[root@localhost ~]# ll -hd /tmp/docker_test2.tar 
-rw-------. 1 root root 280M Nov 25 23:55 /tmp/docker_test2.tar

#查看import帮助信息
[root@localhost ~]# docker import --help

Usage:  docker import [OPTIONS] file|URL|- [REPOSITORY[:TAG]]

Import the contents from a tarball to create a filesystem image

Options:
  -c, --change list       Apply Dockerfile instruction to the created image
  -m, --message string    Set commit message for imported image
      --platform string   Set platform if server is multi-platform capable

#使用import导入tar文件,并修改名称(将容器文件导入为镜像文件)
[root@localhost ~]# cat /tmp/docker_test2.tar | docker import - docker_imtest:1.1
sha256:9ec265f446a211c7f3518aa927ffbc436ee20f12d00352ed8bb02b23bb79902e

4、梳理与总结


export和import 导出的内容是一个容器的快照,并不是镜像本身,没有layer,快照文件会导致容器丢失所有的历史记录与元数据信息,而使用save和load保存的镜像文件则可以保留此两种内容,但是其体积更大;

  • docker load 加载镜像包、 docker import 加载容器包,两者都将其恢复为镜像;
  • docker load不能对镜像重命名、docker import可以为镜像指定新名称;
上一篇:Java 13的新增功能


下一篇:启动报错 Unable to load authentication plugin 'caching_sha2_password'.