我让它正常工作,但现在它停了。我尝试了以下命令,但没有效果:
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也被禁用了
我让它正常工作,但现在它停了。我尝试了以下命令,但没有效果:
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也被禁用了
当前回答
我不知道我在做什么,但这对我很有用:
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
其他回答
对我来说,这是iptables的转发规则。由于某种原因,下面的规则,当与docker的iptables规则结合在一起时,导致所有来自容器的出站流量都到达localhost:8080:
iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-ports 8080
iptables -t nat -I OUTPUT -p tcp -d 127.0.0.1 --dport 80 -j REDIRECT --to-ports 8080
您可能已经使用dns选项启动了docker—dns 172.x.x.x
我有同样的错误,并从/etc/default/docker删除选项
行:
# Use DOCKER_OPTS to modify the daemon startup options.
DOCKER_OPTS="--dns 172.x.x.x"
首先在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复制到容器中。
当我尝试在Ubuntu上使用Docker-Compose建立一个项目时,也遇到了这样的问题。
Docker根本无法访问互联网,当我试图ping任何IP地址或nslookup一些URL时,它总是失败。
我尝试了上面描述的所有可能的DNS解析解决方案,但都无济于事。
我花了一整天的时间试图找出到底发生了什么,最后发现所有问题的原因都是防病毒软件,尤其是它的防火墙,出于某种原因阻止了Docker获取IP地址和端口。
当我禁用它时,一切都很好。
所以,如果你安装了防病毒软件,但没有任何帮助解决这个问题-问题可能是防病毒软件的防火墙。
如果你运行的Docker是无根的,并且遇到了这个问题,在安装过程中,iptables可能没有正确配置,主要是因为在Docker抱怨iptables时使用了——skip-iptables选项:
[ERROR] Missing system requirements. Run the following commands to
[ERROR] install the requirements and run this tool again.
[ERROR] Alternatively iptables checks can be disabled with --skip-iptables .
########## BEGIN ##########
sudo sh -eux <<EOF
# Load ip_tables module
modprobe ip_tables
EOF
########## END ##########
让我们检查一下这是否是问题所在:是否加载了ip_tables内核模块?
sudo modprobe ip_tables
如果没有输出,这个答案可能对您没有帮助(无论如何您都可以尝试)。否则,输出如下所示:
modprobe: FATAL: Module ip_tables not found in directory /lib/modules/5.18.9-200.fc36.x86_64
让我们来解决它!
首先,卸载Docker rootless(不需要通过systemctl停止服务,脚本会处理):
dockerd-rootless-setuptool.sh uninstall --skip-iptables
确保安装了iptables包,尽管它是由主要发行版默认提供的。
现在,让ip_tables模块对modprobe可见并安装它(多亏了这个):
sudo depmod
sudo modprobe ip_tables
现在,重新安装Docker rootless:
dockerd-rootless-setuptool.sh install
如果它不打扰iptables,你就完成了,问题应该得到解决。不要忘记启用服务(即systemctl enable——user——now docker)!