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

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


当前回答

用OSX的答案更新这个问题(使用Docker Machine)

如果你使用Docker Machine在OSX上运行Docker,那么下面的方法对我来说是有效的:

docker-machine restart

<...wait for it to restart, which takes up to a minute...>

docker-machine env
eval $(docker-machine env)

然后(至少在我的经验中),如果你从容器中ping google.com,一切都会很好。

其他回答

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

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

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

经过几个小时的努力,我终于解决了问题

问题是linux使用旧版本的libseccomp2

获取签名密钥以验证新包,否则将无法安装

rpi ~$ sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 04EE7237B7D453EC 648ACFD622F3D138

将Buster后台端口存储库添加到apt sources.list

rpi ~$ echo 'deb http://httpredir.debian.org/debian buster-backports main contrib non-free' | sudo tee -a /etc/apt/sources.list.d/debian-backports.list

rpi ~$ sudo apt update
rpi ~$ sudo apt install libseccomp2 -t buster-backports

在这次尝试之后

rpi ~$ docker run -it --rm alpine:3.15.0
(alpine shell)# apk update

Apk更新将获取,因此您连接到互联网

我在用

Linux raspberrypi 5.10.63-v7l+ #1496 SMP Wed Dec 1 15:58:56 GMT 2021 armv7l GNU/Linux

您可以使用uname -a进行检查

如果你运行的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)!

其他回答指出,docker0接口(桥接)可能是问题的根源。在Ubuntu 20.04上,我观察到接口缺少它的IP地址(用IP addr show dev docker0检查)。仅仅重新启动Docker并没有帮助。我不得不手动删除桥接接口。

sudo ip link delete docker0
sudo systemctl restart docker