【arm 环境】【docker】【centos】 容器不能访问外网,宿主机可以访问外网

本文描述了在CentOS系统中,Docker容器无法访问外网的问题及解决过程。问题出现时,即使宿主机网络正常,容器内的应用也无法联网。经过排查,发现并非防火墙导致的问题,而是Docker网络配置问题。解决方案是重启Docker服务,之后通过`iptables -t nat -L -n`命令查看,发现Docker网络已经可以正常转发数据包,从而解决了容器无法上网的问题。
摘要由CSDN通过智能技术生成

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

问题排查:

  1. docker 是通过与宿主机绑定方式进行网络通信,此时通过宿主机ip a 检查docker 网卡是否有有效ip
  2. 防火墙状态关闭
  3. 检查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方面去考虑。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
如果你使用的是默认的docker网络,那么容器之间是可以相互通信的,但是容器默认是不能被外部访问的。要让外部能够访问容器,可以通过以下几种方法: 1. 使用端口映射:可以将容器内的端口映射到主机的端口上,这样外部就可以通过主机的 IP 地址和端口来访问容器了。例如,将容器内的80端口映射到主机的8080端口上可以使用以下命令: ``` docker run -d -p 8080:80 nginx ``` 2. 使用桥接网络:可以创建一个桥接网络,将容器加入该网络中,这样容器之间就可以相互访问,而且外部也可以通过桥接网络访问容器。例如,创建一个名为`my-net`的桥接网络可以使用以下命令: ``` docker network create my-net ``` 然后将容器加入该网络中: ``` docker run -d --net my-net --name container-1 nginx docker run -d --net my-net --name container-2 nginx ``` 这样容器`container-1`和`container-2`就可以相互访问了,而且外部也可以通过桥接网络访问它们。 3. 使用主机网络:可以使用主机网络模式,将容器直接绑定到主机的网络上,这样容器就可以直接使用主机的 IP 地址访问外部网络。例如,使用主机网络模式启动一个容器可以使用以下命令: ``` docker run -d --net host nginx ``` 这样容器就可以直接使用主机的 IP 地址访问外部网络了。 以上三种方法都可以让外部访问容器,选择哪种方法要根据具体的需求和场景来定。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值