V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
LnTrx
V2EX  ›  NAS

主机模式 Docker 会绕过群晖防火墙?

  •  
  •   LnTrx · 2023-02-12 00:54:08 +08:00 · 2352 次点击
    这是一个创建于 411 天前的主题,其中的信息可能已经有所发展或是发生改变。

    为了能公网访问 IPv6 ,容器选择了 host 模式的网络。然后发现容器内程序绑定的端口可以在外网直接访问,不像 bridge 映射端口的 docker 需要修改防火墙配置。

    想确认一下,主机模式下容器会绕过防火墙,是群晖的特性,还是有配置有误?是否能通过 UI 中的选项修改?(威联通的 QuFireWall 是能挡住的)

    6 条回复    2023-02-12 13:15:59 +08:00
    smallparking
        1
    smallparking  
       2023-02-12 01:11:36 +08:00 via Android
    是的,容器使用主机模式的网络可以绕过主机上的防火墙。

    在主机模式下,容器直接使用主机的网络栈,并且容器上的端口映射到主机上。因此,外部访问者可以直接访问容器上的服务,不会受到主机上的防火墙的限制。

    如果您希望容器的网络连接受到防火墙的保护,则应该使用网络隔离技术,例如:使用网络命名空间、桥接模式、macvlan 等。
    LnTrx
        2
    LnTrx  
    OP
       2023-02-12 01:27:50 +08:00   ❤️ 2
    @smallparking 用 macvlan 恐怕防火墙更挡不住了。不过也好奇,威联通是怎么挡住的,底层设计是什么。
    顺便也想提议,社区应该规定必须标注 AI 回答。
    7v9TEc53
        3
    7v9TEc53  
       2023-02-12 09:49:01 +08:00
    1L 真恶心

    我特意测试了下,有 ipv6 公网 ip ,无 ipv4 公网 ip ,但 docker 显示 host 模式已禁用,群晖防火墙能防住
    lujjjh
        4
    lujjjh  
       2023-02-12 10:48:24 +08:00
    遇到过类似的问题,但不是在群晖上,表现是容器映射出的端口会绕过 ufw 规则限制(还好配置完 ufw 之后测试了一下,不然服务就暴露公网了)。

    原因是容器端口映射是通过 iptables 规则实现的,而 ufw 插入到 iptables 里的规则没有作用于 DOCKER-USER 链,参考:
    https://docs.docker.com/network/iptables/
    https://github.com/docker/for-linux/issues/690

    不知道群晖的防火墙是怎么工作的,可以检查一下 iptables 看看你是不是同样的情况。看了上面的 issue ,我比较意外 --net=host 开放的端口也是走 iptables 的?
    littlewing
        5
    littlewing  
       2023-02-12 11:04:49 +08:00
    **Add iptables policies before Docker’s rules**
    Docker installs two custom iptables chains named DOCKER-USER and DOCKER, and it ensures that incoming packets are always checked by these two chains first.

    All of Docker’s iptables rules are added to the DOCKER chain. Do not manipulate this chain manually. If you need to add rules which load before Docker’s rules, add them to the DOCKER-USER chain. These rules are applied before any rules Docker creates automatically.

    Rules added to the FORWARD chain -- either manually, or by another iptables-based firewall -- are evaluated after these chains. This means that if you expose a port through Docker, this port gets exposed no matter what rules your firewall has configured. If you want those rules to apply even when a port gets exposed through Docker, you must add these rules to the DOCKER-USER chain.
    kenvix
        6
    kenvix  
       2023-02-12 13:15:59 +08:00
    @smallparking 虽然没什么问题,但是这个回答不会是 ChatGPT 吧🤣
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   955 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 21:36 · PVG 05:36 · LAX 14:36 · JFK 17:36
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.