docker - 无法删除docker容器的默认iptables规则

标签 docker firewall iptables

如果我输入 iptables -L 输出中有这一行:

Chain DOCKER (1 references)
target     prot opt source               destination         
ACCEPT     tcp  --  anywhere             172.17.0.2           tcp dpt:http-alt

我的容器是公开的,我可以从任何地方请求一个虚拟的 http 服务器(经过测试)。我尝试删除该规则,因此只有 80 只暴露在我的服务器内( localhost:80 )。我试过了 :
root@ns25252:~# iptables -D DOCKER  --destination 172.17.0.2 -p tcp --dport 80 -j ACCEPT
iptables: Bad rule (does a matching rule exist in that chain?).

正如错误所暗示的那样,它找不到匹配的规则。我应该如何键入以删除该行?

最佳答案

按编号删除通常更容易,除非在您列出规则和删除规则期间编号可能会发生变化。

以下是按行号删除的方法:

# iptables -L --line-numbers
(snip)
Chain DOCKER (2 references)
num  target     prot opt source               destination         
1    ACCEPT     tcp  --  anywhere             172.17.0.2           tcp dpt:http
(snip)
# iptables -D DOCKER 1

或者,您可以通过执行 iptables -S 获得完整的规范。例子:
# iptables -S
(snip)
-A DOCKER -d 172.17.0.2/32 -p tcp -m tcp --dport 80 -j ACCEPT
(snip)

-A 转换为 -D 并将其用作 iptables 的参数以删除规则:
# iptables -D DOCKER -d 172.17.0.2/32 -p tcp -m tcp --dport 80 -j ACCEPT

关于docker - 无法删除docker容器的默认iptables规则,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50084582/

相关文章:

docker - 如何在Docker中使用MariaDB Galera集群调试 “WSREP: SST failed: 1 (Operation not permitted)”?

java - 我如何访问部署在 docker wildfly 镜像上的应用程序

Java服务器套接字: Find an open port NOT blocked by a firewall

docker - 利用 iptables 在 docker 容器之间丢弃数据包

fastApi 中的 python 全局变量无法正常工作

linux - Docker - 在 ENTRYPOINT 中切换到非 root 用户是否安全?

linux - 如何修改下面的squid url_rewriting_program

networking - 如何允许分段数据包通过防火墙(ipv4/v6)?

python - Netfilterqueue 基本使用 Python

linux - 使用 iptables 和 nfqueue 进行数据包转发