本文共 5050 字,大约阅读时间需要 16 分钟。
首先我们启动busybox镜像为容器,在该容器中安装一个小工具,再将这个容器保存为新的镜像
首先我们下载一个镜像,再启动容器
[root@centos ~]# docker pull learn/tutorialUsing default tag: latestTrying to pull repository docker.io/learn/tutorial ... latest: Pulling from docker.io/learn/tutorial271134aeb542: Pull complete Digest: sha256:2933b82e7c2a72ad8ea89d58af5d1472e35dacd5b7233577483f58ff8f9338bdStatus: Downloaded newer image for docker.io/learn/tutorial:latest[root@centos ~]# docker run -d -it --name=tutorial docker.io/learn/tutorial /bin/bash87370a9b56b0810257f8172b82b1e219c15b2a0999ad6703d4da28fc1e5d992f[root@centos ~]# docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES87370a9b56b0 docker.io/learn/tutorial "/bin/bash" 2 minutes ago Up 2 minutes tutorial
进入这个容器
[root@centos ~]# docker exec -it tutorial bashroot@87370a9b56b0:/#
默认tutorial是不带ping工具的,我们下载一个ping
root@87370a9b56b0:/# ping www.baidu.combash: ping: command not foundroot@87370a9b56b0:/# apt-get install -y pingReading package lists... DoneBuilding dependency tree... DoneNote, selecting 'iputils-ping' instead of 'ping'The following NEW packages will be installed: iputils-ping0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.Need to get 56.1 kB of archives.After this operation, 143 kB of additional disk space will be used.Get:1 http://archive.ubuntu.com/ubuntu/ precise/main iputils-ping amd64 3:20101006-1ubuntu1 [56.1 kB]Fetched 56.1 kB in 15s (3580 B/s) debconf: delaying package configuration, since apt-utils is not installedSelecting previously unselected package iputils-ping.(Reading database ... 7545 files and directories currently installed.)Unpacking iputils-ping (from .../iputils-ping_3%3a20101006-1ubuntu1_amd64.deb) ...Setting up iputils-ping (3:20101006-1ubuntu1) ...root@87370a9b56b0:/# ping www.baidu.comPING www.a.shifen.com (115.239.211.112) 56(84) bytes of data.64 bytes from 115.239.211.112: icmp_req=1 ttl=47 time=35.8 ms64 bytes from 115.239.211.112: icmp_req=2 ttl=47 time=35.8 ms^C--- www.a.shifen.com ping statistics ---2 packets transmitted, 2 received, 0% packet loss, time 1001msrtt min/avg/max/mdev = 35.816/35.822/35.828/0.006 ms
先退出容器
root@87370a9b56b0:/# exit exit[root@centos ~]# docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES87370a9b56b0 docker.io/learn/tutorial "/bin/bash" About an hour ago Up About an hour tutorial
保存修改后容器为镜像
[root@centos ~]# docker commit 87370a9b56b0 registry.cn-hangzhou.aliyuncs.com/wzgdxg/newimage:1.0.0sha256:209e769fa98794f7e265fde0dcb78af78dda3abf01ffb0bb1e3dd7d3e248a4d6[root@centos ~]# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEregistry.cn-hangzhou.aliyuncs.com/wzgdxg/newimage 1.0.0 209e769fa987 3 seconds ago 152.8 MBregistry.cn-hangzhou.aliyuncs.com/wzgdxg/testbusybox 1.0.0 14889344ccaa About a minute ago 152.8 MBdocker.io/busybox latest e02e811dd08f 5 weeks ago 1.093 MBdocker.io/learn/tutorial latest a7876479f1aa 3 years ago 128 MB
新的newimage镜像被保存出来了,我们验证newimage是否包含刚装的ping,先删除已经启动的容器,在启动newimage。
[root@centos ~]# docker stop 87370a9b56b087370a9b56b0[root@centos ~]# docker rm 87370a9b56b087370a9b56b0[root@centos ~]# docker run -d -it --name=newimage registry.cn-hangzhou.aliyuncs.com/wzgdxg/newimage:1.0.0 /bin/bash f156289920964f3e9ce3878f7a7d2714361173ebe1105c423e988dfe2ec97264[root@centos ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESf15628992096 registry.cn-hangzhou.aliyuncs.com/wzgdxg/newimage:1.0.0 "/bin/bash" 3 seconds ago Up 3 seconds newimage
进入容器验证下
[root@centos ~]# docker exec -it newimage bashroot@f15628992096:/# ping www.baidu.comPING www.a.shifen.com (115.239.210.27) 56(84) bytes of data.64 bytes from 115.239.210.27: icmp_req=1 ttl=47 time=47.3 ms64 bytes from 115.239.210.27: icmp_req=2 ttl=47 time=47.0 ms^C--- www.a.shifen.com ping statistics ---2 packets transmitted, 2 received, 0% packet loss, time 1001msrtt min/avg/max/mdev = 47.019/47.188/47.358/0.275 ms
[root@centos ~]# docker push registry.cn-hangzhou.aliyuncs.com/wzgdxg/newimage:1.0.0The push refers to a repository [registry.cn-hangzhou.aliyuncs.com/wzgdxg/newimage]91466e7b50f0: Pushed ee1ba0cc9b81: Pushed 1.0.0: digest: sha256:cdcd535beb974a7304c39f19b11bf21f6211441eea65ca445f170b07700bc0ec size: 718
转载地址:http://smbab.baihongyu.com/