如何使用iptables阻止Docker容器端口?

5
我使用Docker服务来设置容器网络,并仅为目标IP打开了端口7035并将其暴露给主机。
当我使用“iptables -nvL”命令检查iptables时,我看到了FORWARD链:
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 DROP       tcp  --  *      *       0.0.0.0/0            172.18.0.2           tcp dpt:7053
1680K  119M DOCKER-ISOLATION  all  --  *      *       0.0.0.0/0            0.0.0.0/0           
1680K  119M DOCKER     all  --  *      br-287ce7f19804  0.0.0.0/0            0.0.0.0/0           
1680K  119M ACCEPT     all  --  *      br-287ce7f19804  0.0.0.0/0            0.0.0.0/0            ctstate RELATED,ESTABLISHED

以及DOCKER链:

Chain DOCKER (4 references)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 ACCEPT     tcp  --  !br-287ce7f19804 br-287ce7f19804  0.0.0.0/0            172.18.0.2           tcp dpt:7053
    0     0 ACCEPT     tcp  --  !br-287ce7f19804 br-287ce7f19804  0.0.0.0/0            172.18.0.2           tcp dpt:7051
    0     0 ACCEPT     tcp  --  !br-287ce7f19804 br-287ce7f19804  0.0.0.0/0            172.18.0.3           tcp dpt:2181
    0     0 ACCEPT     tcp  --  !br-287ce7f19804 br-287ce7f19804  0.0.0.0/0            172.18.0.4           tcp dpt:7053
    0     0 ACCEPT     tcp  --  !br-287ce7f19804 br-287ce7f19804  0.0.0.0/0            172.18.0.4           tcp dpt:7051
    0     0 ACCEPT     tcp  --  !br-287ce7f19804 br-287ce7f19804  0.0.0.0/0            172.18.0.6           tcp dpt:7053
    0     0 ACCEPT     tcp  --  !br-287ce7f19804 br-287ce7f19804  0.0.0.0/0            172.18.0.6  

我想要阻止容器 172.18.0.2 的端口 7053,因此我使用了 sudo iptables -I FORWARD -p tcp -d 172.18.0.2 --dport 7053 -j DROP 命令。但是它并没有起作用。那么,我应该怎么做才能阻止目标 IP 和端口呢?
1个回答

3
以下应该可以工作:
iptables -I DOCKER 1 -p tcp --dport 7053 -j DROP

这将在DOCKER链中所有其他规则之前插入DROP规则。
以下是一些有用的命令:
iptables --list DOCKER -n --line

另外,如果您添加-v(详细)则会获得更多细节。

到目前为止,您可能已经得到了答案,但这可能有助于其他人。


4
Docker的iptables规则应该添加到DOCKER-USER链中,而不是DOCKER链中。 - adrian
就像之前的用户所说 - 来源:https://docs.docker.com/network/iptables/#restrict-connections-to-the-docker-host - Morgy

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接