0%

Docker 网桥 172.17 导致本地无法访问

Docker 安装几个容器后导致本地无法访问,记录解决过程

现象

本地网站无法访问,显示Destination Host Unreachable错误

$ ping xxx.com
PING xxx.com (172.18.0.10) 56(84) bytes of data.
From (172.18.0.1) icmp_seq=1 Destination Host Unreachable
From (172.18.0.1) icmp_seq=2 Destination Host Unreachable
From (172.18.0.1) icmp_seq=3 Destination Host Unreachable

$ route
内核 IP 路由表
目标            网关            子网掩码        标志  跃点   引用  使用 接口
default         _gateway        0.0.0.0         UG    100    0        0 enp0s25
link-local      0.0.0.0         255.255.0.0     U     1000   0        0 enp0s25
172.17.0.0      0.0.0.0         255.255.0.0     U     0      0        0 docker0
172.18.0.0      0.0.0.0         255.255.0.0     U     0      0        0 br-cfc9270b15f3
172.19.0.0      0.0.0.0         255.255.0.0     U     0      0        0 br-f5c5f0da48a2
192.168.110.0   0.0.0.0         255.255.254.0   U     100    0        0 enp0s25

$ ifconfig
br-cfc9270b15f3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.18.0.1  netmask 255.255.0.0  broadcast 172.18.255.255
        inet6 fe80::42:1aff:fec6:7043  prefixlen 64  scopeid 0x20<link>
        ether 02:42:1a:c6:70:43  txqueuelen 0  (以太网)
        RX packets 14  bytes 9546 (9.5 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 797  bytes 91465 (91.4 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

docker0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 172.17.0.1  netmask 255.255.0.0  broadcast 172.17.255.255
        ether 02:42:13:03:9a:6c  txqueuelen 0  (以太网)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

enp0s25: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.110.55  netmask 255.255.254.0  broadcast 192.168.111.255
        inet6 fe80::3615:4135:d778:298  prefixlen 64  scopeid 0x20<link>
        ether 54:ee:75:40:ea:67  txqueuelen 1000  (以太网)
        RX packets 407891  bytes 60596095 (60.5 MB)
        RX errors 0  dropped 15691  overruns 0  frame 0
        TX packets 31293  bytes 3769383 (3.7 MB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
        device interrupt 20  memory 0xf1600000-f1620000  

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (本地环回)
        RX packets 33484889  bytes 2454937345 (2.4 GB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 33484889  bytes 2454937345 (2.4 GB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

问题在于 br-cfc9270b15f3

解决

删除所有容器

$ docker stop $(docker ps -aq)
$ docker rm $(docker ps -aq)

删除network

$ docker network prune
$ ifconfig
docker0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 172.17.0.1  netmask 255.255.0.0  broadcast 172.17.255.255
        ether 02:42:13:03:9a:6c  txqueuelen 0  (以太网)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

enp0s25: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.110.55  netmask 255.255.254.0  broadcast 192.168.111.255
        inet6 fe80::3615:4135:d778:298  prefixlen 64  scopeid 0x20<link>
        ether 54:ee:75:40:ea:67  txqueuelen 1000  (以太网)
        RX packets 419408  bytes 72317482 (72.3 MB)
        RX errors 0  dropped 15691  overruns 0  frame 0
        TX packets 39576  bytes 4970401 (4.9 MB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
        device interrupt 20  memory 0xf1600000-f1620000  

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (本地环回)
        RX packets 34240204  bytes 2501217904 (2.5 GB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 34240204  bytes 2501217904 (2.5 GB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

修改 docker 网段

$ sudo service docker stop
$ sudo ifconfig docker0 down
$ sudo vim /etc/docker/daemon.json
$ sudo service docker restart
$ ifconfig
docker0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 175.168.0.1  netmask 255.255.0.0  broadcast 175.168.255.255
        ether 02:42:13:03:9a:6c  txqueuelen 0  (以太网)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

daemon.json 添加 "bip": "175.168.0.1/16"