docker的暴露api端口2375,没有任何安全防护,我们通过linux系统防火墙(iptables)来进行ip访问限制
# 查看iptables所有规则
iptables -L -nv --line-numbers
# 只允许某个ip访问2375端口
iptables -I INPUT -s 127.0.0.1 -d 127.0.0.1 -p tcp --dport 2375 -j ACCEPT
# 禁止所有ip访问2375端口
iptables -A INPUT -p tcp --dport 2375 -j REJECT
#移除规则命令
iptables -D INPUT -p tcp --dport 2375 -j REJECT
接下来还需要保存iptables的自定义规则,因为系统重启后,会自动重置iptables规则,如果没有持久化并恢复规则,以上指定的规则仍然不会永久生效
#保存规则
iptables-save > /etc/sysconfig/iptables
#恢复规则
iptables-restore < /etc/sysconfig/iptables
这是手动的规则保存恢复操作,我们想要让系统自动恢复,怎么办
#启用rc.local
chmod +x /etc/rc.d/rc.local
systemctl enable rc-local
#编辑自启动命令
vim /etc/rc.local
#添加内容
iptables-restore < /etc/sysconfig/iptables
#测试自启动服务
s