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

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是无根的,并且遇到了这个问题,在安装过程中,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)!

其他回答

首先在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复制到容器中。

对我来说,我在使用podman时遇到了与redhat/centos/fedora用户相同的问题

firewall-cmd --zone=public --add-masquerade
firewall-cmd --permanent --zone=public --add-masquerade

对于更多的防火墙和podman(或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

通过以下建议修复:

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

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

最初我的docker容器能够连接到外部互联网(这是一个运行在Amazon EC2上的docker服务/容器)。

由于我的应用程序是一个API,我随后创建了我的容器(它成功地拉出了所需的所有包),更新了我的IP表,将所有流量从端口80路由到端口,我的API(在docker上运行)正在监听。

后来,当我试图重建容器时,它失败了。经过一番努力,我发现我的上一个步骤(设置IPTable端口转发规则)搞乱了docker的外部网络功能。

解决方案:停止IPTable服务:

Sudo服务iptables停止

重启Docker守护进程:

Sudo服务docker重启

然后,尝试重新构建容器。希望这能有所帮助。


跟进

我完全忽略了一点,我不需要打乱IP表,就可以将进入的流量转发到80,到运行在docker上的API正在运行的端口。相反,我只是将端口80别名为docker中API正在运行的端口:

Docker运行-d -p 80:<api_port> <image>:<tag> <command to start api>