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

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也被禁用了


当前回答

对我来说,我的问题是没有安装iptables-services,这对我来说是有效的(CentOS):

sudo yum install iptables-services
sudo service docker restart

其他回答

我使用的是Arch Linux,在尝试了以上所有的答案后,我意识到我的机器上启用了防火墙,nftables,并禁用它。我说:

sudo systemctl disable nftables
sudo systemctl stop nftables
sudo reboot

我的网卡:

➜  ~ ip link
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: enp1s0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc fq_codel state DOWN mode DEFAULT group default qlen 1000
    link/ether 68:f7:28:84:e7:fe brd ff:ff:ff:ff:ff:ff
3: wlp2s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP mode DORMANT group default qlen 1000
    link/ether d0:7e:35:d2:42:6d brd ff:ff:ff:ff:ff:ff
4: docker0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP mode DEFAULT group default 
    link/ether 02:42:43:3f:ff:94 brd ff:ff:ff:ff:ff:ff
5: br-c51881f83e32: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN mode DEFAULT group default 
    link/ether 02:42:ae:34:49:c3 brd ff:ff:ff:ff:ff:ff
6: br-c5b2a1d25a86: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN mode DEFAULT group default 
    link/ether 02:42:72:d3:6f:4d brd ff:ff:ff:ff:ff:ff
8: veth56f42a2@if7: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master docker0 state UP mode DEFAULT group default 
    link/ether 8e:70:36:10:4e:83 brd ff:ff:ff:ff:ff:ff link-netnsid 0

和我的防火墙配置,/etc/nftables.conf,我现在禁用了,将来会尝试改进,这样我就可以正确设置docker0网卡规则:

#!/usr/bin/nft -f
# vim:set ts=2 sw=2 et:

# IPv4/IPv6 Simple & Safe firewall ruleset.
# More examples in /usr/share/nftables/ and /usr/share/doc/nftables/examples/.

table inet filter
delete table inet filter
table inet filter {
  chain input {
    type filter hook input priority filter
    policy drop

    ct state invalid drop comment "early drop of invalid connections"
    ct state {established, related} accept comment "allow tracked connections"
    iifname lo accept comment "allow from loopback"
    ip protocol icmp accept comment "allow icmp"
    meta l4proto ipv6-icmp accept comment "allow icmp v6"
    #tcp dport ssh accept comment "allow sshd"
    pkttype host limit rate 5/second counter reject with icmpx type admin-prohibited
    counter
  }
  chain forward {
    type filter hook forward priority filter
    policy drop
  }

对我来说,是主机的防火墙。我必须在主机的防火墙上允许DNS。并且在更改主机防火墙设置后不得不重新启动docker。

通过以下建议修复:

[…你能试着重置一切吗?

pkill docker
iptables -t nat -F
ifconfig docker0 down
brctl delbr docker0
docker -d

它将迫使docker重新创建网桥并重新执行所有网络规则

https://github.com/dotcloud/docker/issues/866#issuecomment-19218300

似乎界面以某种方式“挂起”。

更新docker的最新版本:

上面的答案可能仍然可以帮你完成工作,但自从这个答案发布以来已经有很长一段时间了,docker现在更加完善了,所以在使用iptables和所有东西之前,一定要先尝试这些。

Sudo service docker restart或者(如果你的Linux发行版没有使用upstart) Sudo systemctl restart docker

缺少代理设置也可能导致无法上网。在这种情况下,——network host也可能不起作用。代理可以通过设置环境变量http_proxy和https_proxy来配置:

docker run -e "http_proxy=YOUR-PROXY" \
           -e "https_proxy=YOUR-PROXY"\
           -e "no_proxy=localhost,127.0.0.1" ... 

不要忘记设置no_proxy,否则所有请求(包括对localhost的请求)都将通过代理。

更多信息:Archlinux Wiki中的代理设置。

首先在docker容器中运行cat /etc/resolv.conf。如果存在无效的DNS服务器,例如nameserver 127.0.x。X,那么容器将无法将域名解析为IP地址,因此ping google.com将失败。

要检查的第二件事是在主机上运行cat /etc/resolv.conf。每次容器启动时,Docker都会将主机的/etc/resolv.conf文件复制到容器中。所以如果主机的/etc/resolv.conf是错误的,那么docker容器也会错误。

如果你发现主机的/etc/resolv.conf是错误的,那么你有两个选项:

在daemon.json中硬编码DNS服务器。这很简单,但如果您希望DNS服务器更改,则不太理想。 修复主机的/etc/resolv.conf文件。这有点棘手,但它是动态生成的,并且您没有对DNS服务器进行硬编码。


1. 在docker daemon.json中硬编码DNS服务器

编辑/etc/docker/daemon.json { "dns": ["10.1.2.3", "8.8.8.8"] } 重新启动docker守护进程使这些更改生效: Sudo systemctl restart docker 现在当你运行/启动一个容器时,docker会用daemon.json中的值填充/etc/resolv.conf。


2. 修复主机的/etc/resolv.conf文件

A. Ubuntu 16.04及更早版本

对于Ubuntu 16.04及更早的版本,/etc/resolv.conf由NetworkManager动态生成。 注释掉/etc/ networkmanager /NetworkManager.conf中的dns=dnsmasq(带#)行 重启NetworkManager重新生成/etc/resolv.conf: Sudo systemctl restart network manager 在主机上检查:cat /etc/resolv.conf

B. Ubuntu 18.04及以上版本

Ubuntu 18.04 changed to use systemd-resolved to generate /etc/resolv.conf. Now by default it uses a local DNS cache 127.0.0.53. That will not work inside a container, so Docker will default to Google's 8.8.8.8 DNS server, which may break for people behind a firewall. /etc/resolv.conf is actually a symlink (ls -l /etc/resolv.conf) which points to /run/systemd/resolve/stub-resolv.conf (127.0.0.53) by default in Ubuntu 18.04. Just change the symlink to point to /run/systemd/resolve/resolv.conf, which lists the real DNS servers: sudo ln -sf /run/systemd/resolve/resolv.conf /etc/resolv.conf Verify on the host: cat /etc/resolv.conf

现在你应该在主机上有一个有效的/etc/resolv.conf文件供docker复制到容器中。