docker、firewalld和iptables之间的关系

本文探讨了在CentOS7环境下,Docker如何与firewalld服务共存而不冲突,解释了无需关闭firewalld而启动iptables的原因。通过实例说明,即使不使用iptables.service,Docker的端口转发也能正常工作,因为iptables命令一直可用。同时,提供了停用firewalld和启用iptables服务的脚本,以及直接使用firewalld进行端口配置的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

要注意docker命令中使用 -p 暴露端口时,实现需要依赖iptables。CentOS 7默认使用的是firewalld,但是是否需要关闭firewalld并启动iptables呢?

参考多篇博文,答案应该是不不需要的。

Note. You need to distinguish between the iptables service and the iptables command. Although firewalld is a replacement for the firewall management provided by iptables service, it still uses the iptables command for dynamic communication with the kernel packet filter (netfilter). So it is only the iptables service that is replaced, not the iptables command. That can be a confusing distinction at first.

在实际使用过程中,没有使用iptables.service,docker的端口转发也是正常的,因为iptables一直都在。docker会创建自己的iptables链,如果firewalld重启,docker创建的链也需要重新创建。

下面是停用firewalld服务,启用iptables-services
#!/bin/sh

#停止firewalld服务
systemctl stop firewalld    
#禁用firewalld服务
systemctl mask firewalld

yum install -y iptables
yum update iptables
yum install -y iptables-services

systemctl enable iptables.service
systemctl start iptables.service

#暴露docker swarm需要的端口,如果不使用docker swarm不需要打开端口
iptables -A INPUT -p tcp --dport 2377 -j ACCEPT
iptables -A INPUT -p tcp --dport 7946 -j ACCEPT
iptables -A INPUT -p udp --dport 7946 -j ACCEPT
iptables -A INPUT -p tcp --dport 4789 -j ACCEPT
iptables -A INPUT -p udp --dport 4789 -j ACCEPT

service iptables save
systemctl restart iptables.service

#开启转发
echo 'net.ipv4.ip_forward=1'> /usr/lib/sysctl.d/00-system.conf

systemctl restart network

直接使用firewalld

sudo firewall-cmd --permanent --zone=trusted --add-interface=docker0
sudo firewall-cmd --permanent --zone=trusted --add-port=xxxx/tcp#       xxxx改为你希望的端口号
sudo firewall-cmd --reload



 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值