Docker 个容器内没有互联网连接

No internet connection inside Docker containers

我无法在任何 Docker 容器内执行任何需要互联网连接的命令。

作品:

docker run ubuntu /bin/echo 'Hello world'

无效:

docker run ubuntu apt-get update

Err:1 http://archive.ubuntu.com/ubuntu xenial InRelease
  Temporary failure resolving 'archive.ubuntu.com'
Err:2 http://archive.ubuntu.com/ubuntu xenial-updates InRelease
  Temporary failure resolving 'archive.ubuntu.com'
Err:3 http://archive.ubuntu.com/ubuntu xenial-security InRelease
  Temporary failure resolving 'archive.ubuntu.com'
Reading package lists...
W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/xenial/InRelease  Temporary failure resolving 'archive.ubuntu.com'
W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/xenial-updates/InRelease  Temporary failure resolving 'archive.ubuntu.com'
W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/xenial-security/InRelease  Temporary failure resolving 'archive.ubuntu.com'

类似于pipping

我在 Ubuntu 16.04 上,没有使用防火墙或公司代理服务器,并尝试重新启动 Docker。不使用 docker-machine 或 boot2docker.

Got answer on SuperUser:

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

"It will force docker to recreate the bridge and reinit all the network rules"

在我的例子中,仅删除 docker0 接口是不够的,我还必须删除 br 接口(参见 ip link)。

对于使用 systemd 的 Linux 用户:

#!/usr/bin/env sh
if ! [ "$(id -u)" = "0" ]; then
   echo "Run as root please."
   exit 1
fi

# not sure if this is needed
iptables -t nat -F

# stop docker
systemctl stop docker

# stop the `br-...` interfaces.
ip link | awk -F': ' '/: br-/ {print }' | xargs -I % ifconfig % down
# ... and remove them
ip link | awk -F': ' '/: br-/ {print }' | xargs brctl delbr

# stop the docker0 interface
ifconfig docker0 down
# ... and remove it
brctl delbr docker0

# start docker again, this will re-create the docker0 interface.
systemctl start docker

我按照以下顺序解决了它:

  1. 安装 connman 如果之前没有安装:
sudo apt update
sudo apt install connman -y
  1. 关注this solution
sudo nano /etc/connman/main.conf
  1. 重启:
sudo reboot