docker - 如何在自定义网络上允许对 Docker 容器的完全入站和出站 Internet 访问?
问题描述
上周这一直让我陷入困境,因为我的 Docker 环境现在不再工作(在服务器地狱和备份配置无法解决问题之后)。
- 我在 Docker 中创建了一个自定义网络(“自定义”),其内部 IP 范围为 172.18.0.0/16。
- 我所有的 Docker 容器都附加到“自定义”。
- 我有一个主机 DNS 服务器,它对本地域和远程都正常运行(首先使用转发)。
- 我配置了反向代理,根据 IPtables 是否正确,这些代理可以工作。
- 所有 IPtables 配置均已自动生成。我不知道他们是否正确。
我希望能够:
通过 URL(例如 sub.domain.com)连接到我的任何 Docker 容器。
让一些容器能够通过 IP 和端口连接到本地主机上的 MySQL 数据库。
让一些容器能够解析 DNS 并访问互联网。
使用我当前的设置,我无法通过 URL 访问我的容器,但如果我直接访问主机 IP 和暴露的 Docker 端口(即 https://sub.domain.com不起作用,但http://192.168 .1.2:9000 确实)。
容器不连接到配置指向“自定义”网关(即 172.18.0.1:3306)的 MySQL 数据库。
容器无法解析 DNS。DNS 在容器 /etc/resolv.conf 中设置(正确)为 127.0.0.11,然后应将所有请求转发到主机 DNS (192.168.1.2:53)。如果我将 DNS 强制到容器上的网桥网关,那么我可以看到请求到达主机 DNS 但没有被返回。
如果容器在默认网桥上,则 DNS 设置为主机 DNS (192.168.1.2),但不会解析任何请求。如果我将 DNS 强制到 Container 上的网桥网关,那么我可以解析 DNS 请求。
如果有人可以提供帮助,我将不胜感激,因为我以前做过这个工作,但我看不到我可能设置错误的地方!
IPTables 如下:
#FILTER TABLE
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
Chain DOCKER (0 references)
target prot opt source destination
ACCEPT tcp -- anywhere 172.18.0.2 tcp dpt:36330
ACCEPT tcp -- anywhere 172.18.0.2 tcp dpt:7396
ACCEPT tcp -- anywhere 172.17.0.2 tcp dpt:9000
ACCEPT tcp -- anywhere 172.18.0.3 tcp dpt:7999
ACCEPT tcp -- anywhere 172.18.0.3 tcp dpt:7990
Chain DOCKER-ISOLATION-STAGE-1 (0 references)
target prot opt source destination
DOCKER-ISOLATION-STAGE-2 all -- anywhere anywhere
DOCKER-ISOLATION-STAGE-2 all -- anywhere anywhere
RETURN all -- anywhere anywhere
Chain DOCKER-ISOLATION-STAGE-2 (2 references)
target prot opt source destination
DROP all -- anywhere anywhere
DROP all -- anywhere anywhere
RETURN all -- anywhere anywhere
Chain DOCKER-USER (0 references)
target prot opt source destination
RETURN all -- anywhere anywhere
#NAT TABLE
Chain PREROUTING (policy ACCEPT)
target prot opt source destination
Chain INPUT (policy ACCEPT)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
DEFAULT_OUTPUT all -- anywhere anywhere
Chain POSTROUTING (policy ACCEPT)
target prot opt source destination
DEFAULT_POSTROUTING all -- anywhere anywhere
Chain DEFAULT_OUTPUT (1 references)
target prot opt source destination
DOCKER all -- anywhere !127.0.0.0/8 ADDRTYPE match dst-type LOCAL
Chain DEFAULT_POSTROUTING (1 references)
target prot opt source destination
MASQUERADE all -- 172.18.0.0/16 anywhere
MASQUERADE all -- 172.17.0.0/16 anywhere
MASQUERADE tcp -- 172.18.0.2 172.18.0.2 tcp dpt:36330
MASQUERADE tcp -- 172.18.0.2 172.18.0.2 tcp dpt:7396
MASQUERADE tcp -- 172.17.0.2 172.17.0.2 tcp dpt:9000
MASQUERADE tcp -- 172.18.0.3 172.18.0.3 tcp dpt:7999
MASQUERADE tcp -- 172.18.0.3 172.18.0.3 tcp dpt:7990
Chain DOCKER (1 references)
target prot opt source destination
RETURN all -- anywhere anywhere
RETURN all -- anywhere anywhere
DNAT tcp -- anywhere anywhere tcp dpt:36330 to:172.18.0.2:36330
DNAT tcp -- anywhere anywhere tcp dpt:7396 to:172.18.0.2:7396
DNAT tcp -- anywhere anywhere tcp dpt:9000 to:172.17.0.2:9000
DNAT tcp -- anywhere anywhere tcp dpt:7999 to:172.18.0.3:7999
DNAT tcp -- anywhere anywhere tcp dpt:7990 to:172.18.0.3:7990
解决方案
最后,我炸毁了整个服务器并从头开始,还有一些备份。我认为整个配置都是出乎意料的。
推荐阅读
- javascript - Javascript“加载”事件未按预期工作
- python - 在 Python 中,如何找到字符串中表达式之后的子字符串?
- python-3.x - 将 NaN 文本列分隔到其他数据框中
- android - android使用我的VPN ip一个端口将应用程序连接到互联网
- twitter-bootstrap - 我们如何在一个网页上使用多个 Bootstrap CDN?
- outlook-web-addins - Outlook Web 基础插件 - 如何在 Outlook Online 中获取时区设置?
- r - 无法安装 gganimate 依赖项
- neural-network - Is skip-gram model in word2vec an expanded version of N-Gram model? skip-gram vs. skip-grams?
- google-apps-script - How to automatically fill a default value in a sheet cell
- javascript - 如何将输入存储在本地存储中