docker - 即使 UFW 拒绝访问,Docker 端口也会在界面上可见
问题描述
我有一个数字海洋水滴,我使用带有 ubuntu-18-04-x64 图像的 docker-machine 创建。我使用 docker-compose 为其部署了三个容器,其中一个是 nginx,它的端口 80 和 8080 转发到 0.0.0.0 并因此监听所有接口。
nginx:
image: noumena/nrg-vpp/nginx:latest
ports:
- "80:80"
- "8080:8080"
depends_on:
- engine
- api
防火墙配置为阻止 eth0(DO 公共接口)上的所有流量,docker 的 2376 除外,并在 eth1(DO 私有接口)上打开端口 22、80、8080。
22/tcp on eth1 ALLOW Anywhere
8080/tcp on eth1 ALLOW Anywhere
80/tcp on eth1 ALLOW Anywhere
2376/tcp on eth0 ALLOW Anywhere
Anywhere on eth0 DENY Anywhere
Anywhere on eth1 DENY Anywhere
22/tcp (v6) on eth1 ALLOW Anywhere (v6)
8080/tcp (v6) on eth1 ALLOW Anywhere (v6)
80/tcp (v6) on eth1 ALLOW Anywhere (v6)
2376/tcp (v6) on eth0 ALLOW Anywhere (v6)
Anywhere (v6) on eth0 DENY Anywhere (v6)
Anywhere (v6) on eth1 DENY Anywhere (v6)
但是 docker 转发的端口(80,8080)在 eth0 上仍然可见,任何其他端口在 eth 0 上不可见(例如 22)
17:10 $ telnet public-ip 22
Trying x.x.x.x...
telnet: connect to address x.x.x.x: Operation timed out
telnet: Unable to connect to remote host
17:16 $ telnet public-ip 80
Trying x.x.x.x...
Connected to x.x.x.x.
Escape character is '^]'.
^]
telnet> Connection closed.
17:17 $ telnet public-ip 8080
Trying x.x.x.x...
Connected to x.x.x.x.
Escape character is '^]'.
^]
telnet> Connection closed.
为什么这些端口会开放?转发是否在涉及防火墙之前发生?
我通过将容器限制为仅侦听私有 ip 来解决严重问题,但这很烦人,因为它需要额外的逻辑来查询 droplet 的私有 ip,并且 docker-compose 文件成为模板。如果我可以依靠防火墙来关闭转发的端口,那就简单多了。
有没有人遇到过这种行为?我真的很想了解正在发生的事情。
解决方案
这是由于 Docker 处理网络的方式。简而言之,从容器中公开一个端口,您指示 DockerACCEPT
在防火墙(至少对于 UFW)之前为特定端口设置规则,从而有机会应用自己的规则。
GitHub 上跟踪此行为的主要、最明智的问题似乎是#22054。根据您的确切防火墙配置(UFW?),其中一条评论(尤其是较新的一次,自 2017 年左右以来)可能会为您提供解决方案。
另请参阅ServerFault 上类似问题的答案,该答案与#22054 问题上的成功建议之一来自同一个人。
推荐阅读
- react-native - 如何使指示器覆盖选项卡而不是相反
- excel - 如何在Excel VBA中的同一位置组合来自2个数组的条目
- linux - Bash:在列中查找重复项,根据另一列的比较删除行
- typescript - Nuxt - asyncData 错误:“绑定元素 '$axios' 隐式具有 'any' 类型。”
- visual-studio-code - 如何使用 VS Code 为 Windows 做出本机反应?
- c++ - 首次尝试使用 CMake 编译 Open-CV 时出错
- reactjs - 我使用 fetchAPI 和 useState 收到此错误“TypeError:userData.map 不是函数”
- javascript - JavaScript 检查迭代器是否包含值
- c# - 如何将 html 内容从变量加载到 UWP WebView
- json - 包含管道字符的键的 JQ 提取错误