centos7自带防火墙是firewalld。在某下情况下可能造成docker的个别网路问题。
docker有4种网路模式:
1.bridge模式(默认):集线器模式redhat如何关闭防火墙,通过虚拟集线器使容器通讯。容器有自己的独立ip和端口。
2.host模式:主机模式,与主机共用一个网路,容器ip是主机的ip,端口占用主机的端口范围。
3.container模式:与指定容器共享一个网路,类似host模式,并且是两个容器间共用一个ip。
4.none模式:无网路模式,容器有自己的内部网路,并且没有分配ip,路由等信息,须要自己分配。
默认是bridge集线器模式linux内存管理,docker会在宿住机配置一个虚拟网卡linux培训学校,并将容器联接到该网卡,而docker网路与宿住机外部网路的通讯,是利用nat来实现的。所以当Iptable出现问题时,都会造成docker容器网路异常
查看firewalld日志
tail -10 /var/log/firewalld
在日志中可以见到一些警告信息,是docker添加的nat规则,而且由于chain不存在,所以添加失败。通常情况下是没有哪些问题redhat如何关闭防火墙,然而个别情况,例如容器内需要访问宿住机的服务,则可能出现问题。
2019-10-12 17:21:43 WARNING: COMMAND_FAILED: '/usr/sbin/iptables -w10 -t filter -X DOCKER-ISOLATION' failed: iptables: No chain/target/match by that name.
2019-10-12 17:21:43 WARNING: COMMAND_FAILED: '/usr/sbin/iptables -w10 -D FORWARD -i docker0 -o docker0 -j DROP' failed: iptables: Bad rule (does a matching rule exist in that chain?).
2019-10-12 17:21:43 WARNING: COMMAND_FAILED: '/usr/sbin/iptables -w10 -D FORWARD -i br-7407bd71b6f0 -o br-7407bd71b6f0 -j DROP' failed: iptables: Bad rule (does a matching rule exist in that chain?).
解决方案:1.更改docker网路模式为host
在运行容器时添加参数--net=host,而且不能从根本上解决问题。
docker run -d --net=host --name redis redis
2.将firewalld换成iptables(推荐)
#查看firewalld是否启用
systemctl status firewalld
#停止firewalld
systemctl stop firewalld
#禁用firewalld(否则重启系统后会再次启动)
systemctl disable firewalld
#查看是否安装iptables
yum list installed | grep iptables-services
#如果没安装则安装下
yum install iptables-services -y
#重启iptables
systemctl restart iptables
#设置开机自启
systemctl enable iptables
#重启docker
systemctl restart docker
3.关掉防火墙
不推荐,非常是在生产环境
#查看firewalld是否启用
systemctl status firewalld
#停止firewalld
systemctl stop firewalld
#禁用firewalld(否则重启系统后会再次启动)
systemctl disable firewalld