揭秘Docker容器镜像:封装与加载的艺术

揭秘Docker容器镜像:封装与加载的艺术一 容器打包 - 镜像二 镜像打包备份三 从备份镜像包进行镜像加载四 通过加载的备份镜像发布容器


揭秘Docker容器镜像:封装与加载的艺术

一 容器打包 - 镜像

容器提供代码运行环境 + 代码部署完毕 -> ⼀起打包

$ docker commit -m "描述信息" -a "作者信息" 容器ID/容器名称 ⾃定义镜像名称:⾃定义标签
[superman@docker ~]$ docker commit -m "httpd_index" -a "superman" superman01 httpd_superman:1.0

示例:

[superman@docker ~]$ docker ps -a
CONTAINER ID   IMAGE       COMMAND             CREATED       STATUS                       PORTS                                     NAMES
[superman@docker ~]$
[superman@docker ~]$ docker run -d -p 10001:80 --name superman01 httpd:2.4
a49745e02e0326ca496408ca58358779de408356f087189b69d827fe0acf966f
[superman@docker ~]$
[superman@docker ~]$ docker ps -a
CONTAINER ID   IMAGE       COMMAND             CREATED         STATUS         PORTS                                     NAMES
a49745e02e03   httpd:2.4   "httpd-foreground"   14 seconds ago   Up 13 seconds   0.0.0.0:10001->80/tcp, :::10001->80/tcp   superman01
[superman@docker ~]$
[superman@docker ~]$ docker cp ./index.html a49745e02e03:/usr/local/apache2/htdocs
                                            Successfully copied 2.05kB to a49745e02e03:/usr/local/apache2/htdocs
[superman@docker ~]$
[superman@docker ~]$ curl 192.168.0.121:10001

lang=en>

  charset=UTF-8>
  Welcome to Apache




[superman@docker ~]$
[superman@docker ~]$ docker images
REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
tomcat       latest   fb5657adc892   2 years ago   680MB
httpd        2.4       dabbfbe0c57b   2 years ago   144MB
httpd       latest   dabbfbe0c57b   2 years ago   144MB
[superman@docker ~]$
[superman@docker ~]$ docker commit -m "httpd_index" -a "superman" superman01 httpd_superman:1.0
sha256:f0d394fd69d57ec39fc080860e39d5da4c4f9b6702f477d7b033b6e1ed997b10
[superman@docker ~]$
[superman@docker ~]$ docker images
REPOSITORY       TAG       IMAGE ID       CREATED         SIZE
httpd_superman   1.0       f0d394fd69d5   7 seconds ago   144MB
tomcat           latest   fb5657adc892   2 years ago     680MB
httpd            2.4       dabbfbe0c57b   2 years ago     144MB
httpd           latest   dabbfbe0c57b   2 years ago     144MB
[superman@docker ~]$

二 镜像打包备份

$ docker save 镜像名称:标签 -o 镜像打包的⽂件名 (eg: xxx.tar)

docker save文件默认保存在当前目录 - 目录可以手工指定

[superman@docker ~]$ docker save httpd_superman:1.0 -o httpd_superman.tar

示例:

[superman@docker ~]$ ls
index.html superman_share
[superman@docker ~]$
[superman@docker ~]$ docker save httpd_superman:1.0 -o httpd_superman.tar
[superman@docker ~]$
[superman@docker ~]$ ls
httpd_superman.tar index.html superman_share
[superman@docker ~]$

三 从备份镜像包进行镜像加载

[superman@docker ~]$ docker load -i ./httpd_superman.tar

示例:

[superman@docker ~]$ docker stop superman01
superman01
[superman@docker ~]$
[superman@docker ~]$ docker rm superman01  
superman01
[superman@docker ~]$
[superman@docker ~]$ docker images
REPOSITORY       TAG       IMAGE ID       CREATED         SIZE
httpd_superman   1.0       f0d394fd69d5   2 minutes ago   144MB
tomcat           latest   fb5657adc892   2 years ago     680MB
httpd            2.4       dabbfbe0c57b   2 years ago     144MB
httpd           latest   dabbfbe0c57b   2 years ago     144MB
[superman@docker ~]$
[superman@docker ~]$ docker rmi httpd_superman:1.0
Untagged: httpd_superman:1.0
Deleted: sha256:f0d394fd69d57ec39fc080860e39d5da4c4f9b6702f477d7b033b6e1ed997b10
Deleted: sha256:25d542b6f6b60330530bbeefe7fee54394afb8baa2f925a3783b6785da4e7931
[superman@docker ~]$
[superman@docker ~]$ docker images
REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
tomcat       latest   fb5657adc892   2 years ago   680MB
httpd        2.4       dabbfbe0c57b   2 years ago   144MB
httpd       latest   dabbfbe0c57b   2 years ago   144MB
[superman@docker ~]$  
[superman@docker ~]$ docker load -i ./httpd_superman.tar
c48257d11771: Loading layer [==================================================>]  5.632kB/5.632kB
Loaded image: httpd_superman:1.0
[superman@docker ~]$
[superman@docker ~]$ docker images
REPOSITORY       TAG       IMAGE ID       CREATED         SIZE
httpd_superman   1.0       f0d394fd69d5   3 minutes ago   144MB
tomcat           latest   fb5657adc892   2 years ago     680MB
httpd            2.4       dabbfbe0c57b   2 years ago     144MB
httpd           latest   dabbfbe0c57b   2 years ago     144MB
[superman@docker ~]$

四 通过加载的备份镜像发布容器

[superman@docker ~]$ docker run -d -p 11001:8080 --name superman11 httpd_superman:1.0

示例:

[superman@docker ~]$ docker ps -a
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS   PORTS     NAMES
[superman@docker ~]$
[superman@docker ~]$ docker run -d -p 11001:80 --name superman11 httpd_superman:1.0
53d2448b9728b17c4061992ef7e02d9f29513d197702f0404ef046d5d7da06b3
[superman@docker ~]$
[superman@docker ~]$ docker ps -a
CONTAINER ID   IMAGE               COMMAND             CREATED         STATUS         PORTS                                     NAMES
53d2448b9728   httpd_superman:1.0   "httpd-foreground"   4 seconds ago   Up 3 seconds   0.0.0.0:11001->80/tcp, :::11001->80/tcp   superman11
[superman@docker ~]$
[superman@docker ~]$ curl 192.168.0.121:11001

lang=en>

  charset=UTF-8>
  Welcome to Apache




[superman@docker ~]$


? 点赞,你的认可是我创作的动力!

⭐️ 收藏,你的青睐是我努力的方向!

✏️ 评论,你的意见是我进步的财富!


PS:因为公众号平台更改了推送规则,如果不想错过内容,记得读完点一下“在看”,加个“星标”,这样每次新文章推送才会第一时间出现在你的订阅列表里。点“在看”支持我吧!




请使用浏览器的分享功能分享到微信等