【问题标题】:docker makes port of container open to publicdocker使集装箱港口向公众开放
【发布时间】:2021-05-29 08:30:13
【问题描述】:

我在 centos7 docker 容器上安装了 nginx。

端口 80 可从外部使用,尽管此端口未在 firewalld 中打开。 这里的公共区域规则:

# firewall-cmd --zone=public --list-all
public (active)
  target: default
  icmp-block-inversion: no
  interfaces: eno3
  sources:
  services: dhcpv6-client ssh
  ports:
  protocols:
  masquerade: no
  forward-ports:
  sourceports:
  icmp-blocks:
  rich rules:

但在链 DOCKER 中有此端口的自动规则:

# iptables -L DOCKER
Chain DOCKER (1 references)
target     prot opt source               destination
ACCEPT     tcp  --  anywhere             172.17.0.2           tcp dpt:http

如何从外部关闭这个端口? 我已经运行了命令,但没有帮助:

# firewall-cmd --zone=public --remove-port=80/tcp --permanent
Warning: NOT_ENABLED: 80:tcp
success

我已阅读文档 - https://docs.docker.com/engine/userguide/networking/ 但还是不明白。。 实际上我只需要为我指定的网络 1.2.3.4/24 打开这个端口 80。

【问题讨论】:

  • 这个 ip 172.17.0.2 属于 docker 的软件定义网络,不能从主机外部直接访问。您是否尝试过从外部访问它?
  • 如何从外部访问此网络(172.17.0.0/24)?我只能访问 external_ip_docker_host:80 并且儿子不明白如何关闭此访问。

标签: docker containers port centos7 firewalld


【解决方案1】:

Docker 默认设置 iptables 规则。请参阅Docker's Understand container communication 了解更多信息。您也可以在 Docker 守护程序中使用 --iptables=false 禁用此功能。

或者使用 docker-compose 文件版本 2+,您可以使用 expose 关键字而不是 ports 仅在您的 docker-compose.yml 文件中的 Docker 网络内打开此端口。

也就是说,如果你想使用 firewalld,你可能应该禁用 iptables。

【讨论】:

  • 据我所知,我已禁用 iptables:# systemctl status iptables Unit iptables.service 找不到。
  • 如果我使用选项 --iptables=false 我会遇到同样的问题:stackoverflow.com/questions/40792765/… 我无法从我的 docker 容器访问互联网
  • 如果您设置--iptables=false,您将不得不手动配置进出您的 Docker 容器的转发。
【解决方案2】:

可能有点过期了,但是:

您可能正在使用3000:3000 之类的东西进行映射。这非常相当于0.0.0.0:3000:3000

您想要的只是映射到您的本地主机。您可以通过监听特定的 IP 地址来实现这一点,例如 127.0.0.1 (localhost)。

将您的配置更改为127.0.0.1:[host_port]:[container_port]

【讨论】:

  • 这不起作用...有什么想法吗?
猜你喜欢
  • 2019-09-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-08
  • 1970-01-01
  • 2020-05-23
  • 1970-01-01
相关资源
最近更新 更多