创建上图容器的网络环境
- mynet
docker run -d -P --name tomcat07 --network mynet --network-alias tomcat07-mynet leoiceo/centos7-tomcat
1018 docker run -d -P --name tomcat08 --network mynet --network-alias tomcat08-mynet leoiceo/centos7-tomcat
- testnet
docker run -d -P --name tomcat05 --network testnet --network-alias tomcat05-mynet leoiceo/centos7-tomcat
1018 docker run -d -P --name tomcat06 --network testnet--network-alias tomcat06-mynet leoiceo/centos7-tomcat
连接一个容器到一个网络
tomcat05
连接到mynet
docker network connect mynet tomcat05
- 查看tomcat05的网络,一个容器两个IP
"Networks": {
"mynet": {
"IPAMConfig": {},
"Links": null,
"Aliases": [
"6e766490792c"
],
"NetworkID": "1f12b8a96ac34ff8a5f25e94c3bf3f5f454348ddd6f847e2b685e4c7606ff529",
"EndpointID": "1edcb366a88890b155914480d67d2fc7844b8e874a235629a0e2b88d11f4701e",
"Gateway": "192.168.200.1",
"IPAddress": "192.168.200.4",
"IPPrefixLen": 24,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:c0:a8:c8:04",
"DriverOpts": {}
},
"testnet": {
"IPAMConfig": null,
"Links": null,
"Aliases": [
"tomcat05-net",
"6e766490792c"
],
"NetworkID": "f8de1d69de400503565981de021aa1bc068d6f63de532ef8c125348bb6437924",
"EndpointID": "7736e3100cec287def3462c2bc492582a8c8c431fa360ff60b80be8eb0192b3f",
"Gateway": "172.21.0.1",
"IPAddress": "172.21.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:15:00:02",
"DriverOpts": null
}
- 测试连通性,tomcat05 可以ping通mynet下的tomcat07和tomcat08
[root@centos72 ~]# docker network connect mynet tomcat05^C
[root@centos72 ~]# docker exec -it tomcat05 ping tomcat08
PING tomcat08 (192.168.200.3) 56(84) bytes of data.
64 bytes from tomcat08.mynet (192.168.200.3): icmp_seq=1 ttl=64 time=0.379 ms
64 bytes from tomcat08.mynet (192.168.200.3): icmp_seq=2 ttl=64 time=0.106 ms
^C
--- tomcat08 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1000ms
rtt min/avg/max/mdev = 0.106/0.242/0.379/0.137 ms
[root@centos72 ~]# docker exec -it tomcat05 ping tomcat07
PING tomcat07 (192.168.200.2) 56(84) bytes of data.
64 bytes from tomcat07.mynet (192.168.200.2): icmp_seq=1 ttl=64 time=0.235 ms
64 bytes from tomcat07.mynet (192.168.200.2): icmp_seq=2 ttl=64 time=0.117 ms
假设要跨网络操作容器,就需要使用
docker connect
连通