【问题标题】:Restrict connections to the Docker host for several subnets限制多个子网与 Docker 主机的连接
【发布时间】:2021-10-13 16:33:16
【问题描述】:

我需要限制与 docker 容器的连接。 docker documentation 声明如下:

iptables -I DOCKER-USER -i ext_if ! -s 192.168.1.0/24 -j DROP

在此之后,DOCKER-USER 链开始如下所示:

Chain DOCKER-USER (1 references)
target     prot opt source               destination         
DROP       all  -- !192.168.1.0/24       anywhere            
RETURN     all  --  anywhere             anywhere

这工作正常,有来自 192.168.1.0/24 子网的访问,而其他子网没有。但我的问题是我需要添加几个子网。我尝试用几行替换这一行但没有成功

iptables -I DOCKER-USER 1 -i ext_if -s 192.168.1.0/24 -j RETURN
iptables -I DOCKER-USER 2 -i ext_if -s 10.0.0.0/24 -j RETURN
iptables -I DOCKER-USER 3 -j DROP

链条开始看起来像这样:

Chain DOCKER-USER (1 references)
target     prot opt source               destination         
RETURN     all  --  192.168.1.0/24       anywhere            
RETURN     all  --  10.0.0.0/24          anywhere            
DROP       all  --  anywhere             anywhere            
RETURN     all  --  anywhere             anywhere

还尝试了ACCEPT 而不是RETURN,但结果是一样的——所有内容都会被丢弃,无论它来自哪个子网。

有没有办法将工作的一条线分成几条?

附:我知道我可以列出用逗号分隔的子网,但这是不可取的,因为我需要添加大约 15 个子网并为每个子网添加注释。

【问题讨论】:

    标签: docker networking iptables


    【解决方案1】:

    找到解决方案here。 接口应在DROP 行中指定。

    iptables -I DOCKER-USER 1 -i ext_if -s 192.168.1.0/24 -j RETURN
    iptables -I DOCKER-USER 2 -i ext_if -s 10.0.0.0/24 -j RETURN
    iptables -I DOCKER-USER 3 -i ext_if -j DROP
    

    【讨论】:

    • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
    猜你喜欢
    • 2016-06-06
    • 1970-01-01
    • 1970-01-01
    • 2019-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-06
    • 2021-06-14
    相关资源
    最近更新 更多