2.1 Docker镜像管理
2.1.1 案例:构建各类Docker镜像服务
2.1.2 案例环境
主机 | 操作系统 | 主机IP地址 |
服务器 | Centos7.3x86-64 | 192.168.10.100 |
2.1.3 案例拓扑原理
通过 Dockerfile 创建常见应用镜像,Dockerfile 的构成如图 2.3 所示
图 2.1 Dockerfle 构成图
2.1.4 案例需求
下面是本案例的需求:
(1)基于容器(现有镜像)创建镜像:
(2)基于模板创建镜像:
(3)基于 Dockerfile 创建常用基础服务。
1.端口映射
(1)随机映射端口
[root@localhost ~]# docker run -d -P httpd
4c476c9e9567c198a1a2d4f587fd49fa63bdb8efa130b9e0fd91d09692771c9c
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4c476c9e9567 httpd "httpd-foreground" 8 seconds ago Up 7 seconds 0.0.0.0:32768->80/tcp, :::32768->80/tcp hungry_williamson
(2)指定映射端口
[root@localhost ~]# docker run -d -p 8080:80 httpd
b967b6a5eb6c197282ef7b22406364788f398994bb3304bddcf80c635bf28f60
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b967b6a5eb6c httpd "httpd-foreground" 3 seconds ago Up 2 seconds 0.0.0.0:8080->80/tcp, :::8080->80/tcp silly_hypatia
4c476c9e9567 httpd "httpd-foreground" About a minute ago Up About a minute 0.0.0.0:32768->80/tcp, :::32768->80/tcp hungry_williamson
[root@localhost ~]#
2.容器互联
(1)创建源容器
[root@localhost ~]# docker run -dit --name web01 centos:7
5de53ba0244d76ef1f0e89deed62ed1c811cb252a92213166df1d86d4c7d2b6b
(2)创建接收容器
[root@localhost ~]# docker run -dit --name web02 --link web01:web01 centos:7
cc2f113d62752ef7da978df2a0c443240400b5e3b780577b7216de55bbcaf82d
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
cc2f113d6275 centos:7 "/bin/bash" 5 seconds ago Up 5 seconds web02
5de53ba0244d centos:7 "/bin/bash" 59 seconds ago Up 58 seconds web01
b967b6a5eb6c httpd "httpd-foreground" About a minute ago Up About a minute 0.0.0.0:8080->80/tcp, :::8080->80/tcp silly_hypatia
4c476c9e9567 httpd "httpd-foreground" 3 minutes ago Up 3 minutes 0.0.0.0:32768->80/tcp, :::32768->80/tcp hungry_williamson
[root@localhost ~]#
(3)测试容器互联
[root@localhost ~]# docker exec -it web02 /bin/bash
[root@cc2f113d6275 /]# ping web01
PING web01 (172.17.0.4) 56(84) bytes of data.
64 bytes from web01 (172.17.0.4): icmp_seq=1 ttl=64 time=0.127 ms
64 bytes from web01 (172.17.0.4): icmp_seq=2 ttl=64 time=0.076 ms
64 bytes from web01 (172.17.0.4): icmp_seq=3 ttl=64 time=0.071 ms
^C
--- web01 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2048ms
rtt min/avg/max/mdev = 0.071/0.091/0.127/0.026 ms
[root@cc2f113d6275 /]# cat /etc/hosts
127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.17.0.4 web01 5de53ba0244d //web01
172.17.0.5 cc2f113d6275
[root@cc2f113d6275 /]#
3.容器间通信
[root@localhost ~]# docker network create my_net
1a700ddbc5f3b3d479ba0654af19fa87f647a9d1c017e618c6b2d5fe2309379c
[root@localhost ~]# docker run -itd --name web03 centos:7
05b5bec5de8b380a614e85365e719cb6f1d0ea85b10b260bfd10094a6f49ecb6
[root@localhost ~]# docker run -itd --name pc01 --net=my_net centos:7
66896396ab6732e0d4157f5ae20644c76264d75c2789006a2f8385c0e476f1ba
[root@localhost ~]# docker run -itd --name pc02 --net=my_net centos:7
28d589d7761ca23dfec2e5cadc10e726a7736db62f3b3152a365b27166341d47
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
28d589d7761c centos:7 "/bin/bash" 4 seconds ago Up 3 seconds pc02
66896396ab67 centos:7 "/bin/bash" 38 seconds ago Up 37 seconds pc01
05b5bec5de8b centos:7 "/bin/bash" About a minute ago Up About a minute web03
cc2f113d6275 centos:7 "/bin/bash" 6 minutes ago Up 6 minutes web02
5de53ba0244d centos:7 "/bin/bash" 7 minutes ago Up 7 minutes web01
b967b6a5eb6c httpd "httpd-foreground" 8 minutes ago Up 8 minutes 0.0.0.0:8080->80/tcp, :::8080->80/tcp silly_hypatia
4c476c9e9567 httpd "httpd-foreground" 9 minutes ago Up 9 minutes 0.0.0.0:32768->80/tcp, :::32768->80/tcp hungry_williamson
[root@localhost ~]# docker exec -it pc02 /bin/bash
[root@28d589d7761c /]# ping pc01
PING pc01 (172.18.0.2) 56(84) bytes of data.
64 bytes from pc01.my_net (172.18.0.2): icmp_seq=1 ttl=64 time=0.106 ms
64 bytes from pc01.my_net (172.18.0.2): icmp_seq=2 ttl=64 time=0.085 ms
^C
--- pc01 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1051ms
rtt min/avg/max/mdev = 0.085/0.095/0.106/0.014 ms
2.2 Docker数据管理
1.创建数据卷
[root@localhost ~]# docker run -itd -v /data1 -v /data2 --name web04 centos:7
72cdca8369829635566ce9b9c0a3e00a834d8b361743d56413a17754d9eeb7a1
[root@localhost ~]# docker exec -it web04 /bin/bash
[root@72cdca836982 /]# ls
anaconda-post.log bin data1 data2 dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
[root@72cdca836982 /]#
2.数据卷容器
[root@localhost ~]# docker run -itd --volumes-from web04 --name web05 centos:7 /bin/bash
85aa54fdd16585310c913f53803c0f1fb7ef689b481a2a42c1494b27a3a0341a
[root@localhost ~]# docker exec -it web05 /bin/bash
[root@85aa54fdd165 /]# ls
anaconda-post.log bin data1 data2 dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
[root@85aa54fdd165 /]#
3.挂载主句目录作为数据卷
[root@localhost ~]# docker run -dit -p 9999:80 -v /data:/usr/local/apache2/htdocs --name web06 httpd
36eedcd5d5bfeadce269d8c3964d1bd7ebc1f238b91ba5c2502d4bb244a3cd06
[root@localhost ~]# echo "ni hao" > /d
data/ dev/
[root@localhost ~]# echo "ni hao" > /data/index.html
[root@localhost ~]# curl 192.168.10.100:9999
ni hao
[root@localhost ~]#
[root@localhost html]# docker run -itd -p 3333:80 -v /www/conf/default.conf:/etc/nginx/conf.d/default.conf -v /www/html/:/usr/share/nginx/html -v /www/nginx/log/:/var/log/nginx --name web15 nginx /bin/bash
52c2823baf0518a1c63f40a97f6058f035f777dd247beafd85c6aa6aba9454df
[root@localhost html]# docker exec -it web15 /bin/bash
root@52c2823baf05:/# nginx
root@52c2823baf05:/# exit
exit
[root@localhost html]# curl 192.168.10.100:3333
访问成功
[root@localhost html]#