【问题标题】:Can't delete docker container's default iptables rule无法删除 docker 容器的默认 iptables 规则
【发布时间】:2018-10-09 14:36:16
【问题描述】:

如果我输入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?).

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

【问题讨论】:

    标签: docker firewall iptables


    【解决方案1】:

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

    按行号删除的方法如下:

    # 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
    

    注意:令人困惑的是,这个答案仍然不时得到支持。我不知道每个人都在尝试实际完成什么,我只是盲目地回答了一个与 iptables 相关的问题。如果你想启动一个外部世界无法访问的 Docker 容器,那是一个完全不同的话题,这不是你的情况的合适答案。 (也许从不公开/发布端口开始。)

    【讨论】:

    • 您不仅帮我解决了这个问题,而且还提供了一些关于 iptables 的好技巧。为更美好的世界而分享,谢谢。
    • 如何删除 -N 标志
    • @Shqear 嗯?什么 -N 标志?
    • from here。如果您sudo iptables -S | grep -i docker,您将获得四个(新/-N)码头链。删除规则后我们应该-N-F
    • 啊,现在我明白了。是的,如果没有其他规则跳转到该链,-F 将起作用。
    【解决方案2】:

    这有点旧,但如果其他人正在寻找如何从您的 iptables 规则中完全删除 docker,我就是这样做的,还请记住,这是在 debian 上,因此您的文件/路径可能会有所不同。

    1. 编辑您的/etc/iptables.up.rules 文件,备份文件,然后删除其中包含 docker 的所有内容 - 本地 docker 子网可能还有一些额外的行(我的是 172.17.x 和 172.19.x) - 将它们全部删除
    2. 刷新 iptables:iptables -P INPUT ACCEPT && iptables -P OUTPUT ACCEPT && iptables -P FORWARD ACCEPT && iptables -F
    3. 重新加载 iptables 规则:iptables-restore < /etc/iptables.up.rules
    4. 验证/检查您的规则:iptables -L -n(不应再有任何 docker 链或规则)

    【讨论】:

      【解决方案3】:

      如果你删除了 docker 包而不是重启 iptables 服务,它会删除默认的 docker iptables-

      systemctl 重启 iptables.service

      【讨论】:

        猜你喜欢
        • 2018-03-29
        • 1970-01-01
        • 2023-01-26
        • 2014-01-30
        • 1970-01-01
        • 2016-02-23
        • 2020-09-13
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多