iptables 规则在 docker 上不起作用

iptables 规则在 docker 上不起作用

我有一些容器在不同的端口上运行,例如 80、9010 等等。例如:

docker run -d [some other configs] --restart=always -p 9010:443 -p 9010:443/udp xxx/myImage 
#myImage is FROM python:3.6-alpine

我想阻止从主机外部到 9010 的所有连接。我使用了很多命令来限制它,甚至我写了这个:

iptables -A INPUT -p tcp --dport 22 -j ACCEPT # Allow SSH
iptables -A INPUT -j DROP # Drop eveything else

但是它只关闭了本机服务的端口,而不是docker运行的服务。此外,我为DOCKER-USER链写了一些规则:

iptables -I DOCKER-USER -i ext_if ! -s 192.168.1.1 -j DROP

它也没有受到影响,每次我都可以成功地 telnet 到 9010。

最后一次尝试,我禁用 docker-proxy 并启用 iptables /etc/docker/daemon.json

{
    "userland-proxy": false,
    "iptables": true,
    "ipv6": false
}

但不起作用。我检查了很多答案,但没有一个对我有用;例如这个答案。 Docker 遇到防火墙——终于有答案了

答案1

你不需要 iptables 来阻止外部连接。如果你看看例子对于如何公开端口(-p选项),我们可以指定使用哪个 IP 来侦听传入连接。默认是侦听0.0.0.0,这意味着所有 IP。如果您想阻止外部连接,您可以更改该行为。

例如:

docker run -p 127.0.0.1:80:8080/tcp ubuntu bash 

这会将容器的端口 8080 绑定到主机 127.0.0.1(环回接口)上的 TCP 端口 80。

答案2

这是我的 iptables

Chain INPUT (policy ACCEPT)
target     prot opt source               destination

Chain FORWARD (policy DROP)
target     prot opt source               destination
DOCKER-USER  all  --  anywhere             anywhere
DOCKER-ISOLATION-STAGE-1  all  --  anywhere             anywhere
ACCEPT     all  --  anywhere             anywhere             ctstate RELATED,ESTABLISHED
DOCKER     all  --  anywhere             anywhere
ACCEPT     all  --  anywhere             anywhere
ACCEPT     all  --  anywhere             anywhere

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination

Chain DOCKER (1 references)
target     prot opt source               destination
ACCEPT     tcp  --  anywhere             172.17.0.2           tcp dpt:https
ACCEPT     udp  --  anywhere             172.17.0.2           udp dpt:443
ACCEPT     tcp  --  anywhere             172.17.0.3           tcp dpt:https
ACCEPT     tcp  --  anywhere             172.17.0.4           tcp dpt:https
ACCEPT     udp  --  anywhere             172.17.0.4           udp dpt:443
ACCEPT     udp  --  anywhere             172.17.0.3           udp dpt:443
ACCEPT     tcp  --  anywhere             172.17.0.5           tcp dpt:1443
ACCEPT     udp  --  anywhere             172.17.0.5           udp dpt:1443

Chain DOCKER-ISOLATION-STAGE-1 (1 references)
target     prot opt source               destination
DOCKER-ISOLATION-STAGE-2  all  --  anywhere             anywhere
RETURN     all  --  anywhere             anywhere

Chain DOCKER-ISOLATION-STAGE-2 (1 references)
target     prot opt source               destination
DROP       all  --  anywhere             anywhere
RETURN     all  --  anywhere             anywhere

Chain DOCKER-USER (1 references)
target     prot opt source               destination
RETURN     all  --  anywhere             anywhere

为了限制使用docker时传入的数据包,我们必须限制DOCKER链例如:

iptables -A DOCKER -p tcp -d 172.17.0.3 -j DROP

为了限制使用 docker 时传出的数据包,我们可以通过以下方式限制向前像这样的链:

Chain FORWARD (policy DROP)
target     prot opt source               destination
ACCEPT     all  --  172.17.0.3           anywhere             quota: 50000000000 bytes
DROP       all  --  172.17.0.3           anywhere
DOCKER-USER  all  --  anywhere             anywhere
DOCKER-ISOLATION-STAGE-1  all  --  anywhere             anywhere

相关内容