Docker 容器内网络无法连接

操作系统

ubuntu16
docker 参照官方教程安装最新版本

问题描述

最近迷恋上了docker,因为官方提供的ubuntu镜像只有64M!!!十分轻便。
然而基于该镜像生成的容器无法联网,参见下面代码错误,然后试了试其它镜像,依旧无法联网。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
root@gan# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu latest a2a15febcdf3 7 days ago 64.2MB
root@e6a2c5bec004:/# apt update
Err:1 http://security.ubuntu.com/ubuntu bionic-security InRelease
Connection failed [IP: 91.189.88.162 80]
Err:2 http://archive.ubuntu.com/ubuntu bionic InRelease
Connection failed [IP: 91.189.88.149 80]
Err:3 http://archive.ubuntu.com/ubuntu bionic-updates InRelease
Connection failed [IP: 91.189.88.24 80]
Err:4 http://archive.ubuntu.com/ubuntu bionic-backports InRelease
Connection failed [IP: 91.189.88.149 80]
Reading package lists... Done
Building dependency tree
Reading state information... Done
All packages are up to date.
W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/bionic/InRelease Conn ection failed [IP: 91.189.88.149 80]
W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/bionic-updates/InRelea se Connection failed [IP: 91.189.88.24 80]
W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/bionic-backports/InRel ease Connection failed [IP: 91.189.88.149 80]
W: Failed to fetch http://security.ubuntu.com/ubuntu/dists/bionic-security/InRel ease Connection failed [IP: 91.189.88.162 80]
W: Some index files failed to download. They have been ignored, or old ones used instead.

解决办法

例如:
Docker容器内不能联网的6种解决方案
stack overflow中的高分回复 Docker apt-get update fails
中文这六种解决方法,不要轻易动用,除非你之前做过大量的网络设置,负责这些问题基本不会出现。

第一步:首先查看docker虚拟网卡,查看mtu值,如果是1500(默认,或者更大),则需要修改为1450或者更小,/etc/docker/daemon.json 。此外,可以在daemon.json中修改镜像的存储路径,以免占用太多的系统内存;修改mtu值便可以联网了;dns根据情况修改,可以不加吧。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
root@gan:/etc/docker# ifconfig
docker0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1450
inet 172.17.0.1 netmask 255.255.0.0 broadcast 172.17.255.255
inet6 fe80::42:7eff:fee3:87ab prefixlen 64 scopeid 0x20<link>
ether 02:42:7e:e3:87:ab txqueuelen 0 (Ethernet)
RX packets 4518 bytes 281479 (281.4 KB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 5733 bytes 17386562 (17.3 MB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
root@gan# vim /etc/docker/daemon.json
{
"data-root": "/home/ec2-user/software/docker-data",
"storage-driver": "overlay2",
"mtu": 1450,
"dns": ["you_server_dns","8.8.8.8"]
}

##启动
systemctl start docker

## 守护进程重启
sudo systemctl daemon-reload
## 重启docker服务
sudo systemctl restart docker
## 关闭docker
sudo systemctl stop docker

## 重启docker服务
sudo service docker restart
## 关闭docker
sudo service docker stop

第二步:再按照其它方法慢慢修改吧

总结

有时候搜索不到答案,就细致读一下官方文档,按照官方文档操作一般不会遇到问题,否则就是这个软件的BUG了。

MTU [百度百科]
通信术语 最大传输单元(Maximum Transmission Unit,MTU)是指一种通信协议的某一层上面所能通过的最大数据包大小(以字节为单位)。最大传输单元这个参数通常与通信接口有关(网络接口卡、串口等)。

  • 因为协议数据单元的包头和包尾的长度是固定的,MTU越大,则一个协议数据单元的承载的有效数据就越长,通信效率也越高。MTU越大,传送相同的用户数据所需的数据包个数也越低。
  • MTU也不是越大越好,因为MTU越大, 传送一个数据包的延迟也越大;并且MTU越大,数据包中 bit位发生错误的概率也越大。
  • MTU越大,通信效率越高而传输延迟增大,所以要权衡通信效率和传输延迟选择合适的MTU。