Docker&Kubernetes ❀ Docker save load export import 容器镜像的导入与导出方法

网友投稿 690 2022-11-24

docker&Kubernetes ❀ Docker save load export import 容器镜像的导入与导出方法

Docker&Kubernetes ❀ Docker save load export import 容器镜像的导入与导出方法

文章目录

​​1、构建镜像​​​​2、docker save&load​​

​​2.1 save​​​​2.2 load​​

​​3、docker export&import​​

​​3.1 export​​​​3.2 import​​

​​4、梳理与总结​​

1、构建镜像

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

#爬取centos最新版本镜像文件,若已经本地存在,则忽略此步骤[root@localhost ~]# docker pull centos[root@localhost ~]# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEcentos latest 5d0da3dc9764 2 months ago 231MB#使用DockerFile构建新镜像文件,区分与新爬取的镜像[root@localhost ~]# cat /tmp/docker_test FROM centosRUN yum install -y vimRUN yum install -y net-toolsCMD /bin/bash#创建新镜像,镜像版本自定义[root@localhost ~]# docker build -f /tmp/docker_test -t mycentos:1.1 .Sending build context to Docker daemon 10.67MBStep 1/4 : FROM centos ---> 5d0da3dc9764Step 2/4 : RUN yum install -y vim ---> Running in fd8ea201e781Complete!Removing intermediate container fd8ea201e781 ---> 705f34782fe8Step 3/4 : RUN yum install -y net-tools ---> Running in 2b55f92b2400Transaction SummaryComplete!Removing intermediate container 2b55f92b2400 ---> 3cf9004e05dbStep 4/4 : CMD /bin/bash ---> Running in 6f5051dd5df6Removing intermediate container 6f5051dd5df6 ---> a57146ea148cSuccessfully built a57146ea148cSuccessfully tagged mycentos:1.1#查看自定义镜像是否与源镜像存在区别[root@localhost ~]# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEmycentos 1.1 a57146ea148c 6 seconds ago 322MBcentos latest 5d0da3dc9764 2 months ago 231MB

2、docker save&load

2.1 save

#查看save帮助信息[root@localhost ~]# docker save --helpUsage: 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.1Deleted: sha256:a57146ea148c22072e0d38f0a54f1f6980a41a635569e0bd0cbe0ca20a16056aDeleted: sha256:3cf9004e05db213e83d5d2a07437aefdc027ea5a6231975442c53965697e81e8Deleted: sha256:567a09c801ae815e0f77d51d5a53b199d63dff41a7018507403cb20a7c1719c2Deleted: sha256:705f34782fe83875c7b93ff02b355a3d1a0f1254e1999c4473e1060be1c45a8eDeleted: sha256:ea725633ca1e7902c20ad8f8955108e2ce1159f3a74be4cc3918206a5f5fb3d4

2.2 load

#查看load帮助信息[root@localhost ~]# docker load --helpUsage: docker load [OPTIONS]Load an image from a tar archive or STDINOptions: -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.34MB53aeac00a22a: Loading layer [==================================================>] 27.04MB/27.04MBLoaded image: mycentos:1.1#查看导入结果[root@localhost ~]# docker images | grep centosmycentos 1.1 a57146ea148c 8 minutes ago 322MBcentos latest 5d0da3dc9764 2 months ago 231MB

3、docker export&import

3.1 export

#查看export帮助信息[root@localhost ~]# docker export --helpUsage: docker export [OPTIONS] CONTAINERExport a container's filesystem as a tar archiveOptions: -o, --output string Write to a file, instead of STDOUT#使用新镜像创建新容器[root@localhost ~]# docker run -it --name docker_test_001 -d mycentos:1.14f0bddcc627d85e1d1e94ae4b7fb42afcc5cb937f8760ff10b53887122503366#查看新创建的容器信息[root@localhost ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES4f0bddcc627d 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

3.2 import

#查看import帮助信息[root@localhost ~]# docker import --helpUsage: docker import [OPTIONS] file|URL|- [REPOSITORY[:TAG]]Import the contents from a tarball to create a filesystem imageOptions: -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 - mycentos:1.1sha256:9ec265f446a211c7f3518aa927ffbc436ee20f12d00352ed8bb02b23bb79902e#此命令同上条命令,功能一致[root@localhost ~]# docker import /tmp/docker_test2.tar mycentos:1.1#查看导入结果[root@localhost ~]# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEmycentos 1.1 c05c5045a5c1 3 seconds ago 231MB

4、梳理与总结

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

docker load 加载镜像包、 docker import 加载容器包,两者都将其恢复为镜像;docker load不能对镜像重命名、docker import可以为镜像指定新名称;

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:关于spring5的那些事:@Indexed 解密
下一篇:Docker&Kubernetes ❀ Kubernetes集群Pod控制器 - Job
相关文章

 发表评论

暂时没有评论,来抢沙发吧~