首页 > 技术文章 > docker入门(二)

aoeiuv 2016-11-18 15:46 原文

打造自己的镜像

首先我们启动busybox镜像为容器,在该容器中安装一个小工具,再将这个容器保存为新的镜像

首先我们下载一个镜像,再启动容器

[root@centos ~]# docker pull learn/tutorial
Using default tag: latest
Trying to pull repository docker.io/learn/tutorial ... 
latest: Pulling from docker.io/learn/tutorial
271134aeb542: Pull complete 
Digest: sha256:2933b82e7c2a72ad8ea89d58af5d1472e35dacd5b7233577483f58ff8f9338bd
Status: Downloaded newer image for docker.io/learn/tutorial:latest
[root@centos ~]# docker run -d -it --name=tutorial docker.io/learn/tutorial /bin/bash
87370a9b56b0810257f8172b82b1e219c15b2a0999ad6703d4da28fc1e5d992f
[root@centos ~]# docker ps
CONTAINER ID        IMAGE                      COMMAND             CREATED             STATUS              PORTS               NAMES
87370a9b56b0        docker.io/learn/tutorial   "/bin/bash"         2 minutes ago       Up 2 minutes                            tutorial

进入这个容器

[root@centos ~]# docker exec -it tutorial bash
root@87370a9b56b0:/# 

默认tutorial是不带ping工具的,我们下载一个ping

root@87370a9b56b0:/# ping www.baidu.com
bash: ping: command not found
root@87370a9b56b0:/# apt-get install -y ping
Reading package lists... Done
Building dependency tree... Done
Note, selecting 'iputils-ping' instead of 'ping'
The following NEW packages will be installed:
  iputils-ping
0 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 installed
Selecting 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.com
PING 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 ms
64 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 1001ms
rtt min/avg/max/mdev = 35.816/35.822/35.828/0.006 ms

保存修改后的容器为新镜像

先退出容器

root@87370a9b56b0:/# exit                         
exit
[root@centos ~]# docker ps
CONTAINER ID        IMAGE                      COMMAND             CREATED             STATUS              PORTS               NAMES
87370a9b56b0        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.0
sha256:209e769fa98794f7e265fde0dcb78af78dda3abf01ffb0bb1e3dd7d3e248a4d6
[root@centos ~]# docker images
REPOSITORY                                             TAG                 IMAGE ID            CREATED              SIZE
registry.cn-hangzhou.aliyuncs.com/wzgdxg/newimage      1.0.0               209e769fa987        3 seconds ago        152.8 MB
registry.cn-hangzhou.aliyuncs.com/wzgdxg/testbusybox   1.0.0               14889344ccaa        About a minute ago   152.8 MB
docker.io/busybox                                      latest              e02e811dd08f        5 weeks ago          1.093 MB
docker.io/learn/tutorial                               latest              a7876479f1aa        3 years ago          128 MB

新的newimage镜像被保存出来了,我们验证newimage是否包含刚装的ping,先删除已经启动的容器,在启动newimage。

[root@centos ~]# docker stop 87370a9b56b0
87370a9b56b0
[root@centos ~]# docker rm 87370a9b56b0
87370a9b56b0
[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               NAMES
f15628992096        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 bash
root@f15628992096:/# ping www.baidu.com
PING 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 ms
64 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 1001ms
rtt min/avg/max/mdev = 47.019/47.188/47.358/0.275 ms

我们再把newimage推送到阿里云仓库

[root@centos ~]# docker push registry.cn-hangzhou.aliyuncs.com/wzgdxg/newimage:1.0.0
The push refers to a repository [registry.cn-hangzhou.aliyuncs.com/wzgdxg/newimage]
91466e7b50f0: Pushed 
ee1ba0cc9b81: Pushed 
1.0.0: digest: sha256:cdcd535beb974a7304c39f19b11bf21f6211441eea65ca445f170b07700bc0ec size: 718

推荐阅读