[root@docker2 ~]\# docker network ls
NETWORK ID NAME DRIVER SCOPE
56e76e104239 bridge bridge local
af5460d6727a host host local
2305ac12c2f1 none null local
[root@docker2 ~]\# docker network create --help
[root@docker2 ~]\# docker network create -o com.docker.network.bridge.name=docker1 -d bridge --subnet '172.18.0.0/16' bridge-test
32cc657f5b673d989a00555f9f9e5c37d470a5ba4e9b5f24918d163ae364e82e
\# -o:在使用bridge的driver类型时,可以使用-o的附加参数。上面实例中的参数意思是指定创建bridge类型网络时对应虚拟网桥设备的名字。(就是ip a命令看到的名字)
\# -d:指定driver,默认类型就是bridge。
\# --subnet:指定新建的docker网络的网段
\# 最后的bridg-test是即将要将创建出的网络的名字.
[root@docker2 ~]\# docker network ls
NETWORK ID NAME DRIVER SCOPE
56e76e104239 bridge bridge local
32cc657f5b67 bridge-test bridge local
af5460d6727a host host local
2305ac12c2f1 none null local
[root@docker2 ~]\# ip a | grep docker1
19: docker1: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN
inet 172.18.0.1/16 brd 172.16.255.255 scope global docker1
\# 我们以bridge-test网络启动一个容器
[root@docker2 ~]\# docker run --name busy01 -it --network bridge-test --rm busybox
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue qlen 1
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
20: eth0@if21: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue
link/ether 02:42:ac:10:00:02 brd ff:ff:ff:ff:ff:ff
inet 172.18.0.2/16 brd 172.16.255.255 scope global eth0
valid_lft forever preferred_lft forever
\# 另开一个窗口,使用bridge网络再起一个容器
[root@docker2 ~]\# docker run --name busy02 -it --network bridge --rm busybox
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue qlen 1
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
22: eth0@if23: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue
link/ether 02:42:0a:00:00:03 brd ff:ff:ff:ff:ff:ff
inet 172.16.0.2/16 brd 10.0.255.255 scope global eth0
valid_lft forever preferred_lft forever
\# 可以看到两个容器,一个是172.18网段,一个是172.16网段,此时做连通性测试。
\# 不通,此时确定宿主机的ip_forward是否开启,如果开启还不通,则需要另开一个窗口排查防火墙规则。
[root@docker2 ~]\# cat /proc/sys/net/ipv4/ip_forward
[root@docker2 ~]\# iptables -nvL
\# 排查防火墙规则,其实很简单,把target类型为DROP的删掉就好了。我这只列出有DROP的链,并删除
[root@docker2 ~]\# iptables -nvL DOCKER-ISOLATION-STAGE-2 --line-number
Chain DOCKER-ISOLATION-STAGE-2 (2 references)
num pkts bytes target prot opt in out source destination
1 22 1848 DROP all -- * docker1 0.0.0.0/0 0.0.0.0/0
2 0 0 DROP all -- * docker0 0.0.0.0/0 0.0.0.0/0
3 0 0 RETURN all -- * * 0.0.0.0/0 0.0.0.0/0
[root@docker2 ~]\# iptables -D DOCKER-ISOLATION-STAGE-2 2
[root@docker2 ~]\# iptables -D DOCKER-ISOLATION-STAGE-2 1
PING 172.18.0.2 (172.18.0.2): 56 data bytes
64 bytes from 172.18.0.2: seq=0 ttl=63 time=0.262 ms
64 bytes from 172.18.0.2: seq=1 ttl=63 time=0.082 ms