Docker 容器从主机看到不同的 IPTables 规则

Docker Container Sees Different IPTables Rules from Host

我有一个 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

(请注意,我 运行 docker 将 iptables 设置为 false 以尝试对其进行调试,因此它是一组最小的规则,该设置似乎没有有所作为)

接下来在 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 

所以它是一个完全不同的过滤器 table,就像它有自己的副本一样。其他 tables 的类似行为,我已经确认在容器中添加规则不会将它们添加到主机上,即使容器是特权的并且处于主机网络模式。

宿主是raspberry pi运行Raspbian破坏者。我还需要做些什么才能完成这项工作吗?

我早该想到这一点,但我检查了 raspbian 内核版本,它是 4.19.something,这在这一点上已经很古老了。所以我重新安装了 Ubuntu 20.04 服务器(它为 raspberry pi 提供了一个 arm64 发行版),它现在似乎可以按预期工作。很可能这与过时的内核有关。