【发布时间】:2020-09-13 16:20:42
【问题描述】:
我有一个 docker 容器,需要在主机中添加一些 iptables 规则。从搜索来看,这似乎应该在特权模式下工作,或者通过添加 CAP_NET_ADMIN 和 CAP_NET_RAW 并在主机网络模式下工作。
但是,我尝试了这两种方法,无论我做什么,docker 容器似乎都有自己的一套 iptables 规则。这是一个例子:
在主机上iptables -L
Chain INPUT (policy ACCEPT)
target prot opt source destination
Chain FORWARD (policy ACCEPT)
target prot opt source destination
DOCKER-USER all -- anywhere anywhere
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
Chain DOCKER-USER (1 references)
target prot opt source destination
RETURN all -- anywhere anywhere
# Warning: iptables-legacy tables present, use iptables-legacy to see them
(请注意,我在 iptables 设置为 false 的情况下运行 docker 以尝试调试它,因此这是一组最小的规则,该设置似乎没有什么区别)
Ubuntu 容器中的下一步:docker run -it --privileged --net=host ubuntu:18.04 /bin/bash 相同的命令 (iptables -L)
Chain INPUT (policy ACCEPT)
target prot opt source destination
Chain FORWARD (policy ACCEPT)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
所以这是一个完全不同的过滤表,就像它有自己的副本一样。其他表的类似行为,我已经确认在容器中添加规则不会将它们添加到主机上,即使容器具有特权并且处于主机网络模式。
主机是运行 Raspbian buster 的树莓派。我还需要做些什么来完成这项工作吗?
【问题讨论】:
-
Docker 的核心设计目标之一是容器和主机的网络栈是分开的,容器一般不能改变主机的网络配置。在 Docker 之外运行此工具。