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

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)!

其他回答

通过以下建议修复:

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

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

我使用的是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
  }

在分角上, 我的问题是,在启动docker服务之前,我没有安装并启动iptables。在启动docker服务之前,请确保iptables服务已启动并运行。

为子孙后代分享一个简单而有效的解决方案。当我们运行docker容器时,没有显式地提到——network标志,它连接到它的默认网桥网络,这禁止连接到外部世界。为了解决这个问题,我们必须创建自己的网桥网络(用户定义的网桥),并且必须使用docker run命令显式地提到它。

docker network create --driver bridge mynetwork
docker run -it --network mynetwork image:version

我在这里尝试了大部分答案,但唯一有效的是重新创建网络:

$ docker network rm the-network
$ docker network create --driver=bridge the-network

我还需要重新创建使用它的docker容器:

$ sudo docker create --name the-name --network the-network

然后是互联网接入。