我让它正常工作,但现在它停了。我尝试了以下命令,但没有效果:

Docker运行-dns 8.8.8.8 base ping google.com

Docker运行base ping google.com

Sysctl -w net.ipv4。Ip_forward =1 -在主机和容器上

我得到的是未知主机google.com。Docker 0.7.0版本

什么好主意吗?

P.S. ufw也被禁用了


当前回答

最近几天我也遇到了类似的问题。对我来说,原因是systemd、docker和我的托管提供商的组合。我正在运行最新的CentOS(7.7.1908)。

我的主机提供程序自动为systemd-network生成配置文件。从CentOS 7的当前版本systemd 219开始,systemd-network控制了与网络相关的sysctl参数。Docker似乎与这个版本不兼容,并且每次容器启动时都会重置IP-Forwarding标志。

我的解决方案是在提供者生成的配置文件的[Network]部分中添加IPForward=true。这个文件可能在几个地方,最有可能在/etc/systemd/network中。

这个过程在官方docker文档https://docs.docker.com/v17.09/engine/installation/linux/linux-postinstall/#ip-forwarding-problems中也有描述

其他回答

对我来说,使用centos 7.4,这不是/etc/resolve.conf, iptables, iptables NAT规则或docker本身的问题。问题是主机缺少包bridge-utils, docker需要使用brctl命令来构建桥。Yum安装-y bridge-utils并重新启动docker,解决问题。

当我的一个容器随机发生这种情况,而其他容器都很好时,我被难住了。容器至少连接到一个非内部网络,因此Compose定义没有任何问题。重启VM / docker守护进程没有帮助。这也不是DNS问题,因为容器甚至不能ping外部IP。为我解决这个问题的是重新创建docker网络。在我的例子中,docker-compose down && docker-compose up工作。

组成

这迫使我们重新构建所有容器的网络:

Docker-compose down && Docker-compose up

群模式

我认为你只是删除并重新创建服务,这将重新创建服务的网络:

Docker服务rm some-service

Docker服务创建…

如果容器的网络是外部的

只需删除并重新创建该服务的外部网络:

Docker网络rm some-external-network

Docker网络创建一些外部网络

我不知道我在做什么,但这对我很有用:

OTHER_BRIDGE=br-xxxxx # this is the other random docker bridge (`ip addr` to find)    
service docker stop

ip link set dev $OTHER_BRIDGE down
ip link set dev docker0 down
ip link delete $OTHER_BRIDGE type bridge
ip link delete docker0 type bridge
service docker start && service docker stop

iptables -t nat -A POSTROUTING ! -o docker0 -s 172.17.0.0/16 -j MASQUERADE
iptables -t nat -A POSTROUTING ! -o docker0 -s 172.18.0.0/16 -j MASQUERADE

service docker start

在我的例子中,由于一些未知的原因,docker被配置为不生成网络所需的iptables规则容器。docker容器只能ping主机,没有其他功能。这里的大多数答案都没有帮助;重新创建桥或重新启动服务改变了任何东西。

但后来,在一次偶然的机会下,我发现了以下内容:

$ cat /etc/docker/daemon.json
{"iptables": false}

我删除了/etc/docker/daemon.文件Json并重新启动守护进程(默认是创建iptables规则)。这解决了网络问题。

下面是修复前的iptables规则:

$ iptables -L -n
Chain INPUT (policy ACCEPT)
target     prot opt source               destination

Chain FORWARD (policy DROP)
target     prot opt source               destination

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination

现在,iptables规则修复后:

$ iptables -L -n
Chain INPUT (policy ACCEPT)
target     prot opt source               destination

Chain FORWARD (policy DROP)
target     prot opt source               destination
DOCKER-USER  all  --  0.0.0.0/0            0.0.0.0/0
DOCKER-ISOLATION-STAGE-1  all  --  0.0.0.0/0            0.0.0.0/0
ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0            ctstate RELATED,ESTABLISHED
DOCKER     all  --  0.0.0.0/0            0.0.0.0/0
ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0
ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0
ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0            ctstate RELATED,ESTABLISHED
DOCKER     all  --  0.0.0.0/0            0.0.0.0/0
ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0
ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination

Chain DOCKER (2 references)
target     prot opt source               destination
ACCEPT     tcp  --  0.0.0.0/0            172.17.0.2           tcp dpt:8443

Chain DOCKER-ISOLATION-STAGE-1 (1 references)
target     prot opt source               destination
DOCKER-ISOLATION-STAGE-2  all  --  0.0.0.0/0            0.0.0.0/0
DOCKER-ISOLATION-STAGE-2  all  --  0.0.0.0/0            0.0.0.0/0
RETURN     all  --  0.0.0.0/0            0.0.0.0/0

Chain DOCKER-ISOLATION-STAGE-2 (2 references)
target     prot opt source               destination
DROP       all  --  0.0.0.0/0            0.0.0.0/0
DROP       all  --  0.0.0.0/0            0.0.0.0/0
RETURN     all  --  0.0.0.0/0            0.0.0.0/0

Chain DOCKER-USER (1 references)
target     prot opt source               destination
RETURN     all  --  0.0.0.0/0            0.0.0.0/0

最近几天我也遇到了类似的问题。对我来说,原因是systemd、docker和我的托管提供商的组合。我正在运行最新的CentOS(7.7.1908)。

我的主机提供程序自动为systemd-network生成配置文件。从CentOS 7的当前版本systemd 219开始,systemd-network控制了与网络相关的sysctl参数。Docker似乎与这个版本不兼容,并且每次容器启动时都会重置IP-Forwarding标志。

我的解决方案是在提供者生成的配置文件的[Network]部分中添加IPForward=true。这个文件可能在几个地方,最有可能在/etc/systemd/network中。

这个过程在官方docker文档https://docs.docker.com/v17.09/engine/installation/linux/linux-postinstall/#ip-forwarding-problems中也有描述