操作环境

Centos 7

Docker version 17.04.0-ce, build 4845c56

操作步骤

我们发现在docker 宿主机重启后或者docker 服务重启后,docker container的ip会发生改变,所以在docker 内部通过ip从一个container来连接另一台container就不适用了,

这里我们使用--link参数,通过别名的方式来标识一台container。

1.创建两台container,我们等下再重启docker服务后,观察这两台container ip的改变

[root@docker-1 ~]# docker run -dti --name centos6-1 ae5cb7280ec3  
1838d9e0ad114d457bfdf87f5a0f69690515b0397c852778d485a9a7c040b823
[root@docker-1 ~]# docker run -dti --name centos6-2 ae5cb7280ec3  
2b9cee066baf1e3da551df60f26c6817c958ce9a34a6095724f040c8712deb25

2.创建一台container,通过link参数来设置centos6-1 centos6-2两台container的别名,在docker服务重启后,通过别名的方式可以ping通这两台container,分别设置centos6-1,centos6-2对应的别名为webserver-1,webserver-2

[root@docker-1 ~]# docker run -ti --name centos6-3 --link=centos6-1:webserver-1 --link=centos6-2:webserver-2  ae5cb7280ec3  
[root@88ee1d89cd2d /]# 

查看/etc/hosts文件

[root@88ee1d89cd2d /]# 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.3      webserver-1 1838d9e0ad11 centos6-1
172.17.0.4      webserver-2 2b9cee066baf centos6-2
172.17.0.5      88ee1d89cd2d

我们看到webserver-1,webserver-2对应的ip为172.17.0.3,172.17.0.4

4.重启docker服务,再来查看两台container对应的ip

[root@docker-1 ~]# systemctl daemon-reload 
[root@docker-1 ~]# systemctl restart docker
[root@docker-1 ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
a4c364c53f9b        registry            "/entrypoint.sh /e..."   6 days ago          Up 28 seconds       0.0.0.0:5000->5000/tcp   quizzical_bell
[root@docker-1 ~]# docker start centos6-2 centos6-1 centos6-3
centos6-2
centos6-1
centos6-3

查看centos6-3中的/etc/hosts文件

[root@docker-1 ~]# docker attach centos6-3
[root@88ee1d89cd2d /]# 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      webserver-1 1838d9e0ad11 centos6-1
172.17.0.3      webserver-2 2b9cee066baf centos6-2
172.17.0.5      88ee1d89cd2d

这里我们发现webserver-1,webserver-2对应的ip已经发生改变,下面通过ping,检查这两台container是否ping得通

[root@88ee1d89cd2d /]# ping webserver-1      
PING webserver-1 (172.17.0.4) 56(84) bytes of data.
64 bytes from webserver-1 (172.17.0.4): icmp_seq=1 ttl=64 time=0.156 ms
64 bytes from webserver-1 (172.17.0.4): icmp_seq=2 ttl=64 time=0.059 ms
64 bytes from webserver-1 (172.17.0.4): icmp_seq=3 ttl=64 time=0.059 ms
64 bytes from webserver-1 (172.17.0.4): icmp_seq=4 ttl=64 time=0.060 ms
^C
--- webserver-1 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3701ms
rtt min/avg/max/mdev = 0.059/0.083/0.156/0.042 ms
[root@88ee1d89cd2d /]# ping webserver-2
PING webserver-2 (172.17.0.3) 56(84) bytes of data.
64 bytes from webserver-2 (172.17.0.3): icmp_seq=1 ttl=64 time=0.126 ms
64 bytes from webserver-2 (172.17.0.3): icmp_seq=2 ttl=64 time=0.060 ms
64 bytes from webserver-2 (172.17.0.3): icmp_seq=3 ttl=64 time=0.059 ms
64 bytes from webserver-2 (172.17.0.3): icmp_seq=4 ttl=64 time=0.059 ms
64 bytes from webserver-2 (172.17.0.3): icmp_seq=5 ttl=64 time=0.066 ms
^C
--- webserver-2 ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 4903ms
rtt min/avg/max/mdev = 0.059/0.074/0.126/0.026 ms

通过ping检查两台container的别名,都可以ping通。




Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐