【问题标题】:Network unreachable inside docker container without --net=host parameter没有 --net=host 参数的 docker 容器内的网络无法访问
【发布时间】:2016-11-12 17:27:16
【问题描述】:

问题:docker 容器中没有互联网连接。

症状:ping 8.8.8.8 不起作用。来自主机系统的 Wireshark 回馈:

 19 10.866212113   172.17.0.2 -> 8.8.8.8      ICMP 98 Echo (ping) request  id=0x0009, seq=0/0, ttl=64
 20 11.867231972   172.17.0.2 -> 8.8.8.8      ICMP 98 Echo (ping) request  id=0x0009, seq=1/256, ttl=64
 21 12.868331353   172.17.0.2 -> 8.8.8.8      ICMP 98 Echo (ping) request  id=0x0009, seq=2/512, ttl=64
 22 13.869400083   172.17.0.2 -> 8.8.8.8      ICMP 98 Echo (ping) request  id=0x0009, seq=3/768, ttl=64

但是!如果容器以--net=host 启动,互联网将完美运行。

到目前为止我尝试过的:

  • altering DNS
  • --ip-masq=true 添加到/etc/default/docker(关闭重启)
  • 启用与 masquerade / ip_forward 相关的所有内容
  • 更改默认路由
  • 所有建议here

主机配置:

$ sudo route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         10.4.2.1      0.0.0.0         UG    0      0        0 eno1.3001
default         10.3.2.1      0.0.0.0         UG    100    0        0 eno2
10.3.2.0      *               255.255.254.0   U     100    0        0 eno2
10.4.2.0      *               255.255.254.0   U     0      0        0 eno1.3001
nerv8.i         10.3.2.1      255.255.255.255 UGH   100    0        0 eno2
172.17.0.0      *               255.255.0.0     U     0      0        0 docker0

sudo iptables -L, cat /etc/network/interfaces, ifconfig, iptables -t nat -L -nv

一切正常,转发也开启了:

$ sudo sysctl net.ipv4.ip_forward 
net.ipv4.ip_forward = 1

【问题讨论】:

  • docker 的版本(docker --version)、底层主机(uname -a)、您使用的 Dockerimage 的名称以及您用于运行容器的命令是什么?
  • $ docker --version Docker 版本 1.11.2,构建 b9f10c9 $ uname -a Linux 4.4.0-28-generic #47-Ubuntu SMP 16.04

标签: networking docker iptables


【解决方案1】:

这是您正在寻找的不完整答案。但我想解释一下为什么互联网可以工作

如果容器是用 --net=host 启动的,internet 可以工作 完美。

Docker 默认支持三个网络在这种模式下(HOST)容器将共享主机的网络堆栈,并且主机的所有接口都将可供容器使用。容器的主机名将与主机系统上的主机名匹配

# docker run -it --net=host ubuntu:14.04 /bin/bash
root@labadmin-VirtualBox:/# hostname
labadmin-VirtualBox
Even the IP configuration is same as the host system's IP configuration
root@labadmin-VirtualBox:/# ip addr | grep -A 2 eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 08:00:27:b5:82:2f brd ff:ff:ff:ff:ff:ff
    inet 10.0.2.15/24 brd 10.0.2.255 scope global eth0
       valid_lft forever preferred_lft forever
3: lxcbr0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UNKNOWN group default 
root@labadmin-VirtualBox:/# exit
exit

主机系统 IP 配置

# ip addr | grep -A 2 eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 08:00:27:b5:82:2f brd ff:ff:ff:ff:ff:ff
    inet 10.0.2.15/24 brd 10.0.2.255 scope global eth0
       valid_lft forever preferred_lft forever
3: lxcbr0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UNKNOWN group default 

Refer this 了解有关 docker 网络的更多信息。

【讨论】:

    【解决方案2】:

    您能否运行“sudo ifconfig”并查看您的 Internet 连接(通常是 wlan0)的 IP 范围是否与 docker0 接口 172.17.0.0 的范围冲突?

    我的办公室网络(在家工作正常时)遇到了这个问题,它在 172.17.0.X 上运行,而 Docker 试图准确地选择该范围。

    这可能会有所帮助:http://jpetazzo.github.io/2013/10/16/configure-docker-bridge-network/

    我最终为 Docker 创建了自己的桥接网络。

    【讨论】:

      【解决方案3】:

      检查net.ipv4.conf.all.forwarding(不是net.ipv4.ip_forward)是否设置为1,如果没有,打开它:

      $ sysctl net.ipv4.conf.all.forwarding
      
      net.ipv4.conf.all.forwarding = 0
      
      $ sysctl net.ipv4.conf.all.forwarding=1
      
      $ sysctl net.ipv4.conf.all.forwarding
      
      net.ipv4.conf.all.forwarding = 1
      

      【讨论】:

      • 这对我不起作用。另外,我必须使用“--privileged”选项启动容器,否则我会收到“只读文件系统”消息。
      猜你喜欢
      • 2018-12-09
      • 2016-06-01
      • 2020-06-06
      • 2017-11-08
      • 2023-03-03
      • 2018-02-17
      • 1970-01-01
      • 2020-12-17
      • 1970-01-01
      相关资源
      最近更新 更多