(1)创建名centos1的docker
docker run -it --name centos1 centos
创建后查看ip,如下:172.17.0.3
[root@iZbp1flzt6x7pxmxfhmxeeZ ~]# docker run -it --name centos1 centos
[root@52a8e3445ba0 /]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
210: eth0@if211: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
link/ether 02:42:ac:11:00:03 brd ff:ff:ff:ff:ff:ff link-netnsid 0
inet 172.17.0.3/16 brd 172.17.255.255 scope global eth0
valid_lft forever preferred_lft forever
[root@52a8e3445ba0 /]#
(2)创景名centos2 的docker
docker run -it --name centos2 centos
创建后查看ip,如下:172.17.0.5
[root@iZbp1flzt6x7pxmxfhmxeeZ ~]# docker run -it --name centos2 centos
[root@ffd80ec46a5d /]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
212: eth0@if213: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
link/ether 02:42:ac:11:00:05 brd ff:ff:ff:ff:ff:ff link-netnsid 0
inet 172.17.0.5/16 brd 172.17.255.255 scope global eth0
valid_lft forever preferred_lft forever
[root@ffd80ec46a5d /]#
(3)在centos2中分别使用ip和docker名pingcentos1
如下,可以发现,centos2的容器可以ping通centos1的ip,但是无法ping通centos1的容器名
[root@ffd80ec46a5d /]# ping 172.17.0.3 -c 3
PING 172.17.0.3 (172.17.0.3) 56(84) bytes of data.
64 bytes from 172.17.0.3: icmp_seq=1 ttl=64 time=0.095 ms
64 bytes from 172.17.0.3: icmp_seq=2 ttl=64 time=0.085 ms
64 bytes from 172.17.0.3: icmp_seq=3 ttl=64 time=0.073 ms
--- 172.17.0.3 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2000ms
rtt min/avg/max/mdev = 0.073/0.084/0.095/0.011 ms
[root@ffd80ec46a5d /]#
[root@ffd80ec46a5d /]#
[root@ffd80ec46a5d /]# ping centos1 -c 3
ping: centos1: Name or service not known
[root@ffd80ec46a5d /]#
(4)使用--link 创建名centos3的容器,关联centos2
docker run -it --name centos3 --link centos2 centos
创建后查看ip,如:172.17.0.6
[root@iZbp1flzt6x7pxmxfhmxeeZ ~]# docker run -it --name centos3 --link centos2 centos
[root@3cec093832ce /]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
214: eth0@if215: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
link/ether 02:42:ac:11:00:06 brd ff:ff:ff:ff:ff:ff link-netnsid 0
inet 172.17.0.6/16 brd 172.17.255.255 scope global eth0
valid_lft forever preferred_lft forever
[root@3cec093832ce /]#
(5)在centos3 的容器中分别ping容器centos2的ip和容器名
如下可以发现,此时centos3的容器中可以通过centos2的容器名ping通,这就是--link技术
[root@3cec093832ce /]# ping 172.17.0.5 -c 3
PING 172.17.0.5 (172.17.0.5) 56(84) bytes of data.
64 bytes from 172.17.0.5: icmp_seq=1 ttl=64 time=0.102 ms
64 bytes from 172.17.0.5: icmp_seq=2 ttl=64 time=0.065 ms
64 bytes from 172.17.0.5: icmp_seq=3 ttl=64 time=0.062 ms
--- 172.17.0.5 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2000ms
rtt min/avg/max/mdev = 0.062/0.076/0.102/0.019 ms
[root@3cec093832ce /]#
[root@3cec093832ce /]#
[root@3cec093832ce /]# ping centos2 -c 3
PING centos2 (172.17.0.5) 56(84) bytes of data.
64 bytes from centos2 (172.17.0.5): icmp_seq=1 ttl=64 time=0.075 ms
64 bytes from centos2 (172.17.0.5): icmp_seq=2 ttl=64 time=0.071 ms
64 bytes from centos2 (172.17.0.5): icmp_seq=3 ttl=64 time=0.062 ms
--- centos2 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2000ms
rtt min/avg/max/mdev = 0.062/0.069/0.075/0.008 ms
[root@3cec093832ce /]#
(6)容器--link技术的作用
--link技术可以通过容器名访问容器,这就解决了每次启动容器的时候容器的ip都是随机生成的而访问的时候不需要关注随机改变的ip的问题
(7)容器--link技术的本质
在centos3容器中执行如下命令,可以看到--link技术的本质就是在hosts文件中对ip地址进行了设置,这样通过访问容器名的时候自动解析到对应的ip地址
[root@3cec093832ce /]# 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.5 centos2 ffd80ec46a5d
172.17.0.6 3cec093832ce
[root@3cec093832ce /]#