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

2023-02-12 00:54:08 +08:00
 LnTrx

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

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

2407 次点击
所在节点    NAS
6 条回复
smallparking
2023-02-12 01:11:36 +08:00
是的,容器使用主机模式的网络可以绕过主机上的防火墙。

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

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

我特意测试了下,有 ipv6 公网 ip ,无 ipv4 公网 ip ,但 docker 显示 host 模式已禁用,群晖防火墙能防住
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
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
2023-02-12 13:15:59 +08:00
@smallparking 虽然没什么问题,但是这个回答不会是 ChatGPT 吧🤣

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/915279

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX