暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

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

运维家 2022-02-11
160

声明:本文乃“运维家”原创,转载请注明出处,更多内容请关注公众号“运维家”。


主旨

本文主要介绍下如何使用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构建镜像
                    文章转载自运维家,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

                    评论