本文所属【知识林】:http://www.zslin.com/web/article/detail/65

在前面例子中容器与容器之间的通信都是通过网络中的IP地址来完成的,这种方式显然是不合理的,因为这个IP地址可能会在启动容器时发生变化,而且也比较难记。

那么解决这一问题的方法就是使用网络别名,容器在网络是是允许有别名的,且这个别名在所在网络中都可以直接访问,这就类似局域网在各物理机的主机名。

  • 启动容器指定网络及网络别名
docker run -d -it --name c4 --network test-net --network-alias c4-alias centos /bin/bash -c "while true; do echo hello; sleep 1; done"
e51b0ccd566709595c136d3ec41a72652843880184052324e5702bbdc82e0c22

说明:使用--network来指定网络,使用--network-alias来指定网络别名,也就是说c4这个容器在test-net这个网络中的别名是c4-alias,在这个网络中的其他容器可以通过这个别名来访问该容器。

  • 查看网络中的容器
C:\Users\zsl-pc>docker network inspect test-net
…………
"Containers": {
    "0968be0ab042ec35121774227bbecf5c5fd8a1faf0f7fd3b5ac0069af64e7709": {
        "Name": "c3",
        "EndpointID": "1a2062123d61774cd49d074f5dc8705fd0189c24657c7eb2002de4ad908810f0",
        "MacAddress": "02:42:ac:12:00:03",
        "IPv4Address": "172.18.0.3/16",
        "IPv6Address": ""
    },
    "7117f84edc269c5f61052a136791e775e26372c2b977db77081cd78533e5721e": {
        "Name": "c2",
        "EndpointID": "f3c3a919d92c1213da1a13879e75e4e2fe09ed619c65abad0751f8e76f19c2d9",
        "MacAddress": "02:42:ac:12:00:02",
        "IPv4Address": "172.18.0.2/16",
        "IPv6Address": ""
    },
    "e51b0ccd566709595c136d3ec41a72652843880184052324e5702bbdc82e0c22": {
        "Name": "c4",
        "EndpointID": "ab60608e5f26d370b71eb3f4e051f133649de35dff1bae0dcbec17684dca7cb5",
        "MacAddress": "02:42:ac:12:00:04",
        "IPv4Address": "172.18.0.4/16",
        "IPv6Address": ""
    }
}
…………

说明:可以看出现在在test-net中已经有三个容器了。

  • 查看容器详情信息
C:\Users\zsl-pc>docker inspect c4

…………
"Networks": {
    "test-net": {
        "IPAMConfig": null,
        "Links": null,
        "Aliases": [
            "c4-alias",
            "e51b0ccd5667"
        ],
        "NetworkID": "39b38d32553cb89085bccb6d1359e3e1ebba570147c31c64c58e40d7967f9e50",
        "EndpointID": "ab60608e5f26d370b71eb3f4e051f133649de35dff1bae0dcbec17684dca7cb5",
        "Gateway": "172.18.0.1",
        "IPAddress": "172.18.0.4",
        "IPPrefixLen": 16,
        "IPv6Gateway": "",
        "GlobalIPv6Address": "",
        "GlobalIPv6PrefixLen": 0,
        "MacAddress": "02:42:ac:12:00:04"
    }
}
…………

说明:通过查看c4的详情信息可以看出,在Networks里面可以看到该容器所拥有的网络为test-net(这是使用--network来指定的),在test-net里面可以看到Aliases里面有个c4-alias(这是使用--network-alias来指定的),说明c4的网络别名已经设置好了。

  • 通过网络别名与其他容器通信

进入test-net网络中的任意容器(这里选择c3)测试与c4通信:

C:\Users\zsl-pc>docker exec -it c3 /bin/bash
[root@0968be0ab042 /]# ping -w 4 172.18.0.4
PING 172.18.0.4 (172.18.0.4) 56(84) bytes of data.
64 bytes from 172.18.0.4: icmp_seq=1 ttl=64 time=0.071 ms
64 bytes from 172.18.0.4: icmp_seq=2 ttl=64 time=0.074 ms
64 bytes from 172.18.0.4: icmp_seq=3 ttl=64 time=0.122 ms
64 bytes from 172.18.0.4: icmp_seq=4 ttl=64 time=0.079 ms

--- 172.18.0.4 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3001ms
rtt min/avg/max/mdev = 0.071/0.086/0.122/0.022 ms
[root@0968be0ab042 /]# ping -w 4 c4-alias
PING c4-alias (172.18.0.4) 56(84) bytes of data.
64 bytes from c4.test-net (172.18.0.4): icmp_seq=1 ttl=64 time=0.040 ms
64 bytes from c4.test-net (172.18.0.4): icmp_seq=2 ttl=64 time=0.060 ms
64 bytes from c4.test-net (172.18.0.4): icmp_seq=3 ttl=64 time=0.117 ms
64 bytes from c4.test-net (172.18.0.4): icmp_seq=4 ttl=64 time=0.084 ms

--- c4-alias ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3004ms
rtt min/avg/max/mdev = 0.040/0.075/0.117/0.029 ms
[root@0968be0ab042 /]#

说明:可以看出在c3容器中可以通过c4的IP地址或网络别名与之通信。

  • connect的方式来指定网络别名
docker network connect --alias c1-alias test-net c1

说明:使用docker network connect来使容器加入指定网络,通过--alias来指定该容器在指定网络中的别名。

进入c2进进行测试:

C:\Users\zsl-pc>docker exec -it c2 /bin/bash
[root@7117f84edc26 /]# ping -w 2 c1-alias
PING c1-alias (172.18.0.5) 56(84) bytes of data.
64 bytes from c1.test-net (172.18.0.5): icmp_seq=1 ttl=64 time=0.056 ms
64 bytes from c1.test-net (172.18.0.5): icmp_seq=2 ttl=64 time=0.297 ms

--- c1-alias ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1003ms
rtt min/avg/max/mdev = 0.056/0.176/0.297/0.121 ms
[root@7117f84edc26 /]# ping -w 2 c4-alias
PING c4-alias (172.18.0.4) 56(84) bytes of data.
64 bytes from c4.test-net (172.18.0.4): icmp_seq=1 ttl=64 time=0.066 ms
64 bytes from c4.test-net (172.18.0.4): icmp_seq=2 ttl=64 time=0.060 ms

--- c4-alias ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1002ms
rtt min/avg/max/mdev = 0.060/0.063/0.066/0.003 ms

网络别名在整个网络中都是有效的,即在同网络中的任意容器都可通过网络别名访问对应容器。

本文所属【知识林】:http://www.zslin.com/web/article/detail/65
文章点评共:0 条
点评内容: 提交点评