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

问题描述 投票:0回答:1

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

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

主机是一个运行Raspbian buster的raspberry pi。我还需要做什么才能让它正常工作吗?

docker iptables
1个回答
0
投票

我应该早点想到的,但是我查了raspbian的内核版本,是4.19.什么的,这时候已经很古老了。所以我重新安装了Ubuntu 20.04服务器(它为树莓派提供了一个arm64的发行版),现在似乎可以正常工作了。所以很可能是与过时的内核有关。

© www.soinside.com 2019 - 2024. All rights reserved.