自定义网络语法 docker network create –driver bridge –subnet 192.168.0.0/16 –gateway 192.168.0.1 网关名
容器互联 查看所有的Docker网络 1 2 3 4 5 [root@mylinux ~] NETWORK ID NAME DRIVER SCOPE 82e83340755d bridge bridge local 45748c46e01c host host local 5c2b996e0092 none null local
网络模式 bridge : 桥接 docker (默认,自己创建也是用桥接模式)
none :不配置网络
host:和宿主机共享网络
container : 容器网络连通! (用的少局限性很大)
测试 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 [root@mylinux ~] [root@mylinux ~] [root@mylinux ~] 3ad13eb174fcc34449ac4a792921ba8ba0449b1de6deb2de45263aa6b4d66701 [root@mylinux ~] NETWORK ID NAME DRIVER SCOPE 82e83340755d bridge bridge local 45748c46e01c host host local **3ad13eb174fc myway bridge local ** 5c2b996e0092 none null local
我们自己的网络就创建好了
自定义网络的好处呢? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 [root@mylinux ~] PING tocmat02 (192.168.0.3) 56(84) bytes of data. 64 bytes from tocmat02.myway (192.168.0.3): icmp_seq=1 ttl=64 time=0.070 ms 64 bytes from tocmat02.myway (192.168.0.3): icmp_seq=2 ttl=64 time=0.062 ms 64 bytes from tocmat02.myway (192.168.0.3): icmp_seq=3 ttl=64 time=0.068 ms ^C --- tocmat02 ping statistics --- 3 packets transmitted, 3 received, 0% packet loss, time 1001ms rtt min/avg/max/mdev = 0.062/0.066/0.070/0.010 ms [root@mylinux ~] PING tocmat01 (192.168.0.2) 56(84) bytes of data. 64 bytes from tocmat01.myway (192.168.0.2): icmp_seq=1 ttl=64 time=0.051 ms 64 bytes from tocmat01.myway (192.168.0.2): icmp_seq=2 ttl=64 time=0.067 ms 64 bytes from tocmat01.myway (192.168.0.2): icmp_seq=3 ttl=64 time=0.125 ms ^C --- tocmat01 ping statistics --- 3 packets transmitted, 3 received, 0% packet loss, time 3ms rtt min/avg/max/mdev = 0.051/0.081/0.125/0.031 ms
我们使用docker自定义网络 它已经帮我们维护好了关系,推荐使用自定义网络