【arm 环境】【docker】【centos】 容器不能访问外网,宿主机可以访问外网
问题描述:
docker 构建镜像时候对镜像内需要进行资源配置时,发现不能访问外网
注意: 大家在使用docker的时候最好把宿主机的防火墙关了service iptables stop,因为创建容器的时候默认会在防火墙里面添加了docker规则。
cnetos systemctl stop firewalld
firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor >
Active: inactive (dead) since Fri 2021-04-02 09:58:31 CST; 9min ago
Docs: man:firewalld(1)
Process: 20577 ExecStart=/usr/sbin/firewalld --nofork --nopid $FIREWALLD_ARGS>
Main PID: 20577 (code=exited, status=0/SUCCESS)
问题排查:
- docker 是通过与宿主机绑定方式进行网络通信,此时通过宿主机ip a 检查docker 网卡是否有有效ip
- 防火墙状态关闭
- 检查docker ip 有效性
[root@localhost panastor_arm_success]# iptables -t nat -L -n
Chain PREROUTING (policy ACCEPT)
target prot opt source destination
Chain INPUT (policy ACCEPT)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
Chain POSTROUTING (policy ACCEPT)
target prot opt source destination
发现此时docker 此时网络的数据包是不能访问外部网络的
解决办法: 重启docker服务:service docker restart
iptables -t nat -L -n
[root@localhost ~]# iptables -t nat -L -n
Chain PREROUTING (policy ACCEPT)
target prot opt source destination
DOCKER all -- 0.0.0.0/0 0.0.0.0/0 ADDRTYPE match dst-type LOCAL
Chain INPUT (policy ACCEPT)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
DOCKER all -- 0.0.0.0/0 !127.0.0.0/8 ADDRTYPE match dst-type LOCAL
Chain POSTROUTING (policy ACCEPT)
target prot opt source destination
MASQUERADE all -- 172.17.0.0/16 0.0.0.0/0
Chain DOCKER (2 references)
target prot opt source destination
RETURN all -- 0.0.0.0/0 0.0.0.0/0
此时docker 这个ip 段可以上网
个人觉得docker是通过桥接绑定宿主机ip方式进行的网络通信,dns 配置问题可能性不大。所以并没从dns方面去考虑。