ITPub博客

首页 > 云计算 > Docker/K8 > 07 揭秘Docker容器镜像:封装与加载的艺术

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

原创 Docker/K8 作者:shuchaoyang 时间:2024-11-05 21:59:32 0 删除 编辑

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

07 揭秘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

<html lang =en>
<head>
   <meta charset =UTF-8>
   <title>Welcome to Apache</title>
</head>
<body>
</body>
</html>
[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

<html lang =en>
<head>
   <meta charset =UTF-8>
   <title>Welcome to Apache</title>
</head>
<body>
</body>
</html>
[superman@docker ~] $

原文链接: https://mp.weixin.qq.com/s?__biz=MzkxNzI1OTE3Mw==&mid=2247492133&idx=1&sn=921a4d223c616a3589942e2dcb74ee34&chksm=c141f4eff6367df96da4bc5bc37f97e9ce6ebf41e05f4c0db5cba5167ef5048a96f97e0958f1&token=2097529203&lang=zh_CN#rd

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

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

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

图片



来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/291614/viewspace-3045292/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论
专注于IT系统集成、运维、架构二十年的IT职场老兵,拥有CCNA、OCA、OCP、PGCA、PGCE认证证书,公众号:超哥的IT私房菜,知识星球:超哥的IT私房菜,目前是多家IT厂商的咨询顾问。

注册时间:2021-08-27

  • 博文量
    552
  • 访问量
    303473