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

93883ef620dd14bad812961fe38ef271.jpeg

主旨

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

环境

linux环境
ddocker环境

概念

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

启动容器

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

新增命令ifconfig

[root@c84f1f4e5c37 /]# yum -y install net-tools
[root@c84f1f4e5c37 /]# ifconfig
eth0: 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/yunweijia
while 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 images
REPOSITORY    TAG       IMAGE ID       CREATED          SIZE
centos        ceshi     adeae577d8b3   13 seconds ago   359MB
hello-world   latest    feb5d9fea6a5   4 months ago     13.3kB
centos        7         eeb6ee3f44bd   4 months ago     204MB
[yunweijia@localhost ~]$

用新镜像启动容器

[yunweijia@localhost ~]$ sudo docker run -d centos:ceshi /bin/bash -c yunweijia
291ac4a08a8d7f746ec7690e92241bf17c683edb2b5694fb421ba7ba067d9725
[yunweijia@localhost ~]$ 
[yunweijia@localhost ~]$ sudo docker ps
CONTAINER ID   IMAGE          COMMAND                  CREATED          STATUS         PORTS     NAMES
291ac4a08a8d   centos:ceshi   "/bin/bash -c yunwei…"   8 seconds ago    Up 7 seconds             awesome_wiles

看下新容器的日志

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

进入容器中,看下新增的ifconfig命令是否存在

[yunweijia@localhost ~]$ sudo docker exec -it 291ac4a08a8d /bin/bash
[root@291ac4a08a8d /]# ifconfig
eth0: 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构建镜像

a32bf4dec9cc7ad6e5781a6e06837ee1.jpeg

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

运维家

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值