# 通过--link 可以解决 (base) root@linux:/home/cpss# docker exec -it tomcat03 ping tomcat02 PING tomcat02 (172.17.0.3) 56(84) bytes of data. 64 bytes from tomcat02 (172.17.0.3): icmp_seq=1 ttl=64 time=0.108 ms 64 bytes from tomcat02 (172.17.0.3): icmp_seq=2 ttl=64 time=0.066 ms 64 bytes from tomcat02 (172.17.0.3): icmp_seq=3 ttl=64 time=0.069 ms
# 反向 ping不通 (base) root@linux:/home/cpss# docker exec -it tomcat02 ping tomcat03 ping: tomcat03: Name or service not known
(base) root@linux:/home/cpss# docker network ls NETWORK ID NAME DRIVER SCOPE 27abb1e4b0d3 bridge bridge local e69e785a705e host host local 2412989a4eb3 none null local
(base) root@linux:/home/cpss# docker network --help Commands: connect Connect a container to a network create Create a network disconnect Disconnect a container from a network inspect Display detailed information on one or more networks ls List networks prune Remove all unused networks rm Remove one or more networks
1 2 3 4 5
(base) root@linux:/home/cpss# docker network ls NETWORK ID NAME DRIVER SCOPE 27abb1e4b0d3 bridge bridge local e69e785a705e host host local 2412989a4eb3 none null local
# 我们可以自定义个网络 (base) root@linux:/home/cpss# docker network create --help Options: --attachable Enable manual container attachment --aux-address map Auxiliary IPv4 or IPv6 addresses used by Network driver (default map[]) --config-from string The network from which to copy the configuration --config-only Create a configuration only network -d, --driver string Driver to manage the Network (default "bridge") --gateway strings IPv4 or IPv6 Gateway for the master subnet --ingress Create swarm routing-mesh network --internal Restrict external access to the network --ip-range strings Allocate container ip from a sub-range --ipam-driver string IP Address Management Driver (default "default") --ipam-opt map Set IPAM driver specific options (default map[]) --ipv6 Enable IPv6 networking --label list Set metadata on a network -o, --opt map Set driver specific options (default map[]) --scope string Control the network's scope --subnet strings Subnet in CIDR format that represents a network segment docker network create --driver bridge --subnet 192.168.0.0/16 --gateway 192.168.0.1 mynet
不使用—link 也能连接了
我们自定义的网络docker都已经帮我们维护好了对应的关系
好处:
不同的集群使用不同的网络,保证集群是安全健康的。
网络连通
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
(base) root@linux:/home/cpss# docker network --help connect Connect a container to a network (base) root@linux:/home/cpss# docker network connect --help Options: --alias strings Add network-scoped aliasfor the container --driver-opt strings driver options for the network --ip string IPv4 address (e.g., 172.30.100.104) --ip6 string IPv6 address (e.g., 2001:db8::33) --link list Add link to another container --link-local-ip strings Add a link-local address for the container