【问题标题】:Why starting a docker container changes the host's default route?为什么启动 docker 容器会改变主机的默认路由?
【发布时间】:2016-12-11 09:34:09
【问题描述】:

我已经为我的主机配置了以下路由表:

user@host:~ $ netstat -rn
Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
{VPN SERVER IP} 192.168.2.1     255.255.255.255 UGH       0 0          0 wlan0
172.17.0.0      0.0.0.0         255.255.0.0     U         0 0          0 docker0
192.168.2.0     0.0.0.0         255.255.255.0   U         0 0          0 wlan0

所以如果没有连接到 VPN,我就不会连接到互联网:

user@host:~ $ ping google.com
connect: Network is unreachable

一旦我启动我的 docker 容器,主机的路由表就会更改为:

user@host:~ $ netstat -rn
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
0.0.0.0         192.168.2.1     0.0.0.0         UG        0 0          0 wlan0
{VPN SERVER IP} 192.168.2.1     255.255.255.255 UGH       0 0          0 wlan0
169.254.0.0     0.0.0.0         255.255.0.0     U         0 0          0 docker0
169.254.0.0     0.0.0.0         255.255.0.0     U         0 0          0 vethcbeee28
172.17.0.0      0.0.0.0         255.255.0.0     U         0 0          0 docker0
192.168.2.0     0.0.0.0         255.255.255.0   U         0 0          0 wlan0

我又连接到互联网了:

user@host:~ $ ping google.com
PING google.com (216.58.212.238) 56(84) bytes of data.

基本上我的主机不应该能够在没有连接到 VPN 的情况下连接到互联网。但是,启动容器会再次设置到我的网关的默认路由。

有人知道这里发生了什么吗?还有,如何避免呢?

到目前为止,我找到了一个解决方法here,无论如何我都想避免。

编辑:

我刚刚发现即使从 dockerfile 构建图像也会发生这种情况!

【问题讨论】:

    标签: networking docker routing


    【解决方案1】:

    我也遇到了同样的问题,终于找到了解决办法:

    # Stop and disable dhcpcd daemon on system boot since we going to start it manually with /etc/rc.local
    # NB: we do so, cause 'docker' when building or running a container sets up a 'bridge' interface which interferes 'failover'
    systemctl stop dhcpcd
    systemctl disable dhcpcd
    
    # Start dhcpcd daemon on each interface we are interested in
    dhcpcd eth0
    dhcpcd eth1
    dhcpcd wlan0
    
    # Start dhcpcd daemon on every reboot
    sed -i -e 's/^exit 0$//g' /etc/rc.local
    echo "dhcpcd eth0" >> /etc/rc.local
    echo "dhcpcd eth1" >> /etc/rc.local
    echo "dhcpcd wlan0" >> /etc/rc.local
    echo "" >> /etc/rc.local
    echo "exit 0" >> /etc/rc.local
    

    我还为 docker 添加了 dns 服务器(可能没有必要)

    cat >> /etc/docker/daemon.json << EOF
    {
        "dns": ["8.8.8.8", "8.8.4.4"]
    }
    EOF
    service docker restart
    

    【讨论】:

      【解决方案2】:

      您可以在/etc/dhcpd.conf 文件中指定nogateway 选项。

      # Avoid to set the default routes.
      nogateway
      

      【讨论】:

      • 我遇到了类似的问题。 @dario,除了上面提到的,你有没有找到解决方案?
      猜你喜欢
      • 2016-08-21
      • 2023-03-03
      • 2021-01-23
      • 2018-02-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多