04-docker-commit构建自定义镜像

主旨

本文主要介绍下如何使用commit方式来自定义构建镜像,本文只需浅尝辄止,因为这种方式不是主流构建镜像的方式,所以知道有这种方式即可。

环境
linux环境ddocker环境

概念
容器里面的操作,当容器被销毁了就没有了最好不要把数据存放在容器中我们的实际需求,基础镜像很多无法满足,所以我们需要自定义构建镜像

启动容器
[yunweijia@localhost ~]$ sudo docker run -it centos:7 /bin/bash

新增命令ifconfig
[root@c84f1f4e5c37 /]# yum -y install net-tools[root@c84f1f4e5c37 /]# ifconfigeth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500        inet 172.17.0.2  netmask 255.255.0.0  broadcast 172.17.255.255        ether 02:42:ac:11:00:02  txqueuelen 0  (Ethernet)        RX packets 8  bytes 648 (648.0 B)        RX errors 0  dropped 0  overruns 0  frame 0        TX packets 0  bytes 0 (0.0 B)        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 loop txqueuelen 1000 (Local Loopback) RX packets 0 bytes 0 (0.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 0 bytes 0 (0.0 B) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[root@c84f1f4e5c37 /]#

新增脚本

增加一个可持续运行的脚本。,新增的脚本所处位置,必须在容器中PATH的目录下方可,或者修改下PATH环境变量,不然无法在生成容器的时候指定该脚本,后面配置程序也是如此。

[root@c84f1f4e5c37 /]# vi /usr/bin/yunweijiawhile true;do echo yunweijia; sleep 5; done[root@c84f1f4e5c37 /]# exit  # 退出容器

构建镜像
语法:docker commit 容器ID 镜像名:版本号  参数解释:    镜像名:随意,和原镜像无任何关系    版本号:随意,和原版本无任何关系实例:[yunweijia@localhost ~]$ sudo docker commit c84f1f4e5c37 centos:ceshi[yunweijia@localhost ~]$ sudo docker imagesREPOSITORY    TAG       IMAGE ID       CREATED          SIZEcentos        ceshi     adeae577d8b3   13 seconds ago   359MBhello-world   latest    feb5d9fea6a5   4 months ago     13.3kBcentos        7         eeb6ee3f44bd   4 months ago     204MB[yunweijia@localhost ~]$

用新镜像启动容器
[yunweijia@localhost ~]$ sudo docker run -d centos:ceshi /bin/bash -c yunweijia291ac4a08a8d7f746ec7690e92241bf17c683edb2b5694fb421ba7ba067d9725[yunweijia@localhost ~]$ [yunweijia@localhost ~]$ sudo docker psCONTAINER ID   IMAGE          COMMAND                  CREATED          STATUS         PORTS     NAMES291ac4a08a8d   centos:ceshi   "/bin/bash -c yunwei…"   8 seconds ago    Up 7 seconds             awesome_wiles

看下新容器的日志
[yunweijia@localhost ~]$ sudo docker logs 291ac4a08a8dyunweijiayunweijiayunweijiayunweijiayunweijiayunweijia[yunweijia@localhost ~]$   # 说明我们指定的脚本在容器中运行呢

进入容器中,看下新增的ifconfig命令是否存在
[yunweijia@localhost ~]$ sudo docker exec -it 291ac4a08a8d /bin/bash[root@291ac4a08a8d /]# ifconfigeth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500        inet 172.17.0.3  netmask 255.255.0.0  broadcast 172.17.255.255        ether 02:42:ac:11:00:03  txqueuelen 0  (Ethernet)        RX packets 8  bytes 648 (648.0 B)        RX errors 0  dropped 0  overruns 0  frame 0        TX packets 0  bytes 0 (0.0 B)        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 loop txqueuelen 1000 (Local Loopback) RX packets 0 bytes 0 (0.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 0 bytes 0 (0.0 B) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[root@291ac4a08a8d /]#

至此,用commit构建镜像结束。
下一篇:使用dockerfile构建镜像

如果本文内容对你有所帮助,请关注并分分享我的公众号【运维家】。

标签

发表评论