④. 自定义网络 解决域名互通问题
①. 创建网络以及查看网络
docker network create --driver bridge --subnet 192.168.0.0/16 --gateway
docker network ls
# 创建网络 # --driver bridge桥接方式 # --subnet 子网范围 # --gateway 网关 [root@i-id8g0yu9 ~]docker network create --driver bridge --subnet 192.168.0.0/16 --gateway 192.168.0.1 mynet [root@i-id8g0yu9 ~]# docker network ls NETWORK ID NAME DRIVER SCOPE e5cd143c03d2 bridge bridge local f32331eb0b47 host host local 78640edcbc51 mynet bridge local a034131e72f2 none null local
②. 给容器指定网络
docker run -d -P --name mytomcat2 --network=mynet tomcat:jre8-alpine
"Networks": { "mynet": { "IPAMConfig": null, "Links": null, "Aliases": [ "b52789c95d6b" ], "NetworkID": "78640edcbc51ba5eae3989e44ff88b9481bab7c171323c2ba4c90425b18cd31b", "EndpointID": "2ce04296a84e5f79da2a2494ca24efdc387a4ec7976f6529984f76501e1c70bc", "Gateway": "192.168.0.1", "IPAddress": "192.168.0.2", "IPPrefixLen": 16, "IPv6Gateway": "", "GlobalIPv6Address": "", "GlobalIPv6PrefixLen": 0, "MacAddress": "02:42:c0:a8:00:02", "DriverOpts": null } }
③. docker run -it --name myalpine6 --network container:mytomcat alpine
[root@i-id8g0yu9 ~]# docker run -it --name myalpine6 --network container:mytomcat alpine / # ping mytomcat PING mytomcat (192.168.0.2): 56 data bytes 64 bytes from 192.168.0.2: seq=0 ttl=64 time=0.042 ms 64 bytes from 192.168.0.2: seq=1 ttl=64 time=0.088 ms 64 bytes from 192.168.0.2: seq=2 ttl=64 time=0.110 ms ^C --- mytomcat ping statistics --- 3 packets transmitted, 3 packets received, 0% packet loss round-trip min/avg/max = 0.042/0.080/0.110 ms / # ip addr 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN 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 351: eth0@if352: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue state UP link/ether 02:42:c0:a8:00:02 brd ff:ff:ff:ff:ff:ff inet 192.168.0.2/16 brd 192.168.255.255 scope global eth0 valid_lft forever preferred_lft forever / #
④. 跨网络连接别人就用。把mytomcat3加入到mynet网络
docker run -d -P --name mytomcat3 tomcat:jre8-alpine # docker network connect [OPTIONS] NETWORK CONTAINER docker network connect mynet mytomcat3