使用 iptables 和 ipset 为 docker 容器设置 IP 白名单

使用 iptables 和 ipset 为 docker 容器设置 IP 白名单

我想要限制仅从指定 IP 访问 docker 容器端口。

我使用 ipset 设置了 iptables 规则。

我已经暴露了8888端口,来自8888端口的请求会被转发到简单的docker web服务器。

我定义了带有白名单 IP 地址的 ipset。

ipset create testfilter iphash
ipset add testfilter 192.168.52.65

然后我创建了 iptables 规则。新链名为testfilterFORWARD规则的端口8888应跳转到testfilter。链中的第一条规testfilter则应与白名单 ipset 中的 IP 匹配。当 IP 与白名单不匹配时,链中的第二条规testfilter则应丢弃通信。

iptables -N testfilter
iptables -I FORWARD -p tcp --dport 8888 -j testfilter
iptables -A testfilter -m set --match-set test_ips src -j RETURN
iptables -A testfilter -j DROP

但端口8888仍可从任何 IP 访问。我还尝试使用INPUT规则而不是规则,还从规则中FORWARD删除参数并用参数代替它。有什么想法可以解决这个问题吗?--dport-m state --state NEW

答案1

您可以访问 8888 端口,因为您的 docker 容器正在 localhost 上运行,并且 FORWARD 没有阻止它。请使用 -i 选项指定 lo 接口。

相关内容