Docker&Kubernetes ❀ Docker save load export import 容器镜像的导入与导出方法
Posted 无糖可乐没有灵魂
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Docker&Kubernetes ❀ Docker save load export import 容器镜像的导入与导出方法相关的知识,希望对你有一定的参考价值。
文章目录
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
2.1 save
#查看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
2.2 load
#查看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
3.1 export
#查看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
3.2 import
#查看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 - mycentos:1.1
sha256:9ec265f446a211c7f3518aa927ffbc436ee20f12d00352ed8bb02b23bb79902e
#此命令同上条命令,功能一致
[root@localhost ~]# docker import /tmp/docker_test2.tar mycentos:1.1
#查看导入结果
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mycentos 1.1 c05c5045a5c1 3 seconds ago 231MB
4、梳理与总结
export和import 导出的内容是一个容器的快照,并不是镜像本身,没有layer,快照文件会导致容器丢失所有的历史记录与元数据信息,而使用save和load保存的镜像文件则可以保留此两种内容,但是其体积更大;
- docker load 加载镜像包、 docker import 加载容器包,两者都将其恢复为镜像;
- docker load不能对镜像重命名、docker import可以为镜像指定新名称;
以上是关于Docker&Kubernetes ❀ Docker save load export import 容器镜像的导入与导出方法的主要内容,如果未能解决你的问题,请参考以下文章
Docker&Kubernetes ❀ Docker 容器技术笔记链接梳理
Docker&Kubernetes ❀ Kubernetes集群实践与部署笔记知识点梳理
Docker&Kubernetes ❀ Kubernetes集群安装部署过程与常见的错误解决方法
Docker&Kubernetes ❀ Kubernetes集群安装部署过程与常见的错误解决方法