如果连接到桥接网络,则无法从 docker 容器内部进行 apt-get 更新

Kla*_*aus 8 networking ubuntu debian apt docker

我试图理解为什么我无法apt-get update从 docker 容器内运行debian:latestubuntu:latest.

  • 我可以ping 8.8.8.8从容器内或其他地址
  • 我能够ping google.com从容器内或其他域
  • apt-get update如果我用以下命令启动容器就可以了--network host
  • apt-get updatebridge如果容器连接到默认网络或任何其他用户定义的网络且适配器设置为桥接,则不起作用
  • 主机是openstack管理的虚拟机
  • 一切都在我的计算机上按预期运行(不是 openstack,不是虚拟的)

apt-get update将超时:

root@66230c3e7572:/# apt update
Err:1 http://deb.debian.org/debian buster InRelease                           
  Connection failed [IP: 199.232.138.132 80]
Err:2 http://security.debian.org/debian-security buster/updates InRelease
  Connection failed [IP: 151.101.194.132 80]
Err:3 http://deb.debian.org/debian buster-updates InRelease              
  Connection failed [IP: 199.232.138.132 80]
Reading package lists... Done
Building dependency tree       
Reading state information... Done
All packages are up to date.
W: Failed to fetch http://deb.debian.org/debian/dists/buster/InRelease  Connection failed [IP: 199.232.138.132 80]
W: Failed to fetch http://security.debian.org/debian-security/dists/buster/updates/InRelease  Connection failed [IP: 151.101.194.132 80]
W: Failed to fetch http://deb.debian.org/debian/dists/buster-updates/InRelease  Connection failed [IP: 199.232.138.132 80]
W: Some index files failed to download. They have been ignored, or old ones used instead.
Run Code Online (Sandbox Code Playgroud)

我想找出为什么当我连接到虚拟机上的apt-get udpate任何网络时无法工作。bridge因此,非常感谢有关如何调试此问题的任何提示。

Kla*_*aus 6

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

docker 桥接网络的 MTU 必须与主机网络适配器的 MTU 匹配

在我的例子中,eth0(主机)的 MTU 设置为 1450,而 docker0 的 MTU 设置为 1500

您可以通过以下任一方式更改 MTU

  • 按照/sf/ask/2471034821/中所述编辑 docker.service
  • 添加“mtu”:到/etc/docker/daemon.jsonsystemctl restart docker

如果您没有/etc/docker/daemon.json直接创建一个:

# /etc/docker/daemon.json
# adjust the MTU accordingly to the hosts network adapter

{
    "mtu":1450
}
Run Code Online (Sandbox Code Playgroud)

不要忘记重新启动 docker.service:systemctl restart docker.service

更多细节:

如果您不想检查设置,请使用ip并比较 MTU 值

$ ip a

2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> **mtu 1450** qdisc fq_codel state UP group default qlen 1000
    ...
3: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> **mtu 1500** qdisc noqueue state DOWN group default 
    ...
Run Code Online (Sandbox Code Playgroud)

请注意,docker0 状态始终为 1500,并且仅当容器连接到该网络时才更改其值

$ ip a
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1450 qdisc fq_codel state UP group default qlen 1000
    ...
3: docker0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1450 qdisc noqueue state UP group default 
    ...
17: vethe4b452f@if16: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1450 qdisc noqueue master docker0 state UP group default 
    ...
Run Code Online (Sandbox Code Playgroud)

定制网络

我还尝试仅创建一个已定义的自定义网络,MTU而不是设置MTUvia /etc/docker/daemon.json这不起作用,我不知道为什么

docker network create --opt com.docker.network.mtu=1450 CustomMTU