阻止外部访问 docker 容器

Chr*_*ris 3 iptables docker centos7

我想阻止从外部直接访问 docker 容器。我使用 haproxy,只允许访问端口 80、443。

我在 iptables 中添加了以下规则。但我仍然可以通过不同的端口访问 docker 容器。

*filter
:INPUT DROP [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 443 -j ACCEPT
COMMIT
Run Code Online (Sandbox Code Playgroud)

这可能是由于 DOCKER 链造成的

# iptables -L
Chain INPUT (policy DROP)
target     prot opt source               destination
ACCEPT     all  --  anywhere             anywhere             state RELATED,ESTABLISHED
ACCEPT     icmp --  anywhere             anywhere
ACCEPT     all  --  anywhere             anywhere
ACCEPT     tcp  --  anywhere             anywhere             state NEW tcp dpt:ssh
ACCEPT     tcp  --  anywhere             anywhere             state NEW tcp dpt:http
ACCEPT     tcp  --  anywhere             anywhere             state NEW tcp dpt:https

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

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination

Chain DOCKER (4 references)
target     prot opt source               destination
ACCEPT     tcp  --  anywhere             172.18.0.2           tcp dpt:http

Chain DOCKER-ISOLATION (1 references)
target     prot opt source               destination
DROP       all  --  anywhere             anywhere
DROP       all  --  anywhere             anywhere
DROP       all  --  anywhere             anywhere
DROP       all  --  anywhere             anywhere
DROP       all  --  anywhere             anywhere
DROP       all  --  anywhere             anywhere
DROP       all  --  anywhere             anywhere
DROP       all  --  anywhere             anywhere
DROP       all  --  anywhere             anywhere
DROP       all  --  anywhere             anywhere
DROP       all  --  anywhere             anywhere
DROP       all  --  anywhere             anywhere
RETURN     all  --  anywhere             anywhere
Run Code Online (Sandbox Code Playgroud)

我需要创建什么规则来阻止直接访问?

小智 5

您可以使用该docker network create NETWORK命令创建一个网络来连接您的应用程序和代理,而不是使用 IP 表来执行此操作。也不要在任何端口上公开应用程序。您应该公开的唯一容器是您的代理。然后,您可以在代理内部使用容器名称作为主机名来路由流量。同一网络上的每个容器都可以被其他容器访问。

例如如果

  • 我有容器 A,其名称为my-service并在端口 3000 上运行服务,并且没有发布到主机的端口
  • 容器 B 是在发布到主机的80 端口上运行的代理。我的代理可以将请求传递到http://my-service:3000并将流量路由到容器。
  • 如果我尝试访问http://mydomain:3000这将无法工作,因为端口尚未公开,访问应用程序的唯一方法是通过端口 80 上的代理

我建议阅读https://docs.docker.com/engine/userguide/networking/work-with-networks/因为这解释了如何开始使用网络。

完全披露:我在我的个人 VPS 上运行这种设置,无法直接通过端口访问我的容器。使用内置的 docker 网络可能比乱搞 IP 表更好

希望这有用。

迪伦

编辑

我概括了该过程,因为我不知道您在代理、网络限制等方面的设置细节。我也没有进入具体命令,因为上面的链接比我更好地涵盖了它。