Docker 网络禁用: 警告: IPv4转发禁用。网络无法工作

主机中的容器“突然”与外部世界的容器失去连接。然而,一些主机被更新,突然我们遇到了以下情况:

  1. 主机可以与其他主机通信。
  2. 在主机中运行的容器无法与其他主机通信。

这里有一个例子:

[root@pprdespap322 deploy]# ping ci.docker.company.net
PING pprdespap324.corp.company.net (10.137.55.22) 56(84) bytes of data.
64 bytes from pprdespap324.corp.company.net (10.137.55.22): icmp_seq=1 ttl=64 time=0.282 ms
64 bytes from pprdespap324.corp.company.net (10.137.55.22): icmp_seq=2 ttl=64 time=0.341 ms
^C
--- pprdespap324.corp.company.net ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1001ms
rtt min/avg/max/mdev = 0.282/0.311/0.341/0.034 ms

现在,从容器本身,我们不能 ping 同一个主机:

[root@pprdespap322 deploy]# docker run -ti quay.io/coreos/registry ping ci.docker.company.net
WARNING: IPv4 forwarding is disabled. Networking will not work.
ping: unknown host ci.docker.company.net

我第一次看到这个警告是在 Docker 的初始版本中... ... 有了 Docker 1.9.1和1.10.3,如何解决这个问题?

150593 次浏览

我复习了 http://chrisgilmerproj.github.io/ubuntu/network/docker/2013/09/05/ipv4-forwarding-and-docker.html,它帮助我解决了宿主的问题。

我在 /etc/sysctl.conf中添加了以下内容:

net.ipv4.ip_forward=1

然后我重新启动了网络服务并验证了设置:

[root@pprdespap322 deploy]#  systemctl restart network
[root@pprdespap322 deploy]# sysctl net.ipv4.ip_forward
net.ipv4.ip_forward = 1
[root@pprdespap322 deploy]# docker run -ti quay.io/coreos/registry ping ci.docker.company.net
PING pprdespap324.corp.company.net (10.137.55.22) 56(84) bytes of data.
64 bytes from pprdespap324.corp.company.net (10.137.55.22): icmp_seq=1 ttl=63 time=0.329 ms
64 bytes from pprdespap324.corp.company.net (10.137.55.22): icmp_seq=2 ttl=63 time=0.306 ms
^C
--- pprdespap324.corp.company.net ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1001ms
rtt min/avg/max/mdev = 0.306/0.317/0.329/0.021 ms

所有集装箱现在可以与外界集装箱通信!

尝试添加 --network=hostdocker run命令来解决这个问题。

Https://medium.com/@gchandra/docker-ipv4-forwarding-is-disabled-8499ce59231e

试试重启 Docker 服务。

例如 Ubuntu:

$ sudo systemctl restart docker

解决了重启网络的问题。

systemctl restart network

对我有效的方法是:

# vim /etc/sysctl.conf
net.ipv4.ip_forward=1
# sysctl -p
# docker restart container_name/id