【发布时间】:2020-06-06 20:52:49
【问题描述】:
我有一堆 Docker 容器(实际上是 Apache Superset 系统)。我使用 docker-compose 运行它们,如下所示:
$ docker-compose run
docker-compose.yml 文件可以从官方 Apache Superset 存储库中获取。问题是 - 我无法创建与运行 dockerized Apache Superset 的同一主机上运行的 Postgresql 数据库的新数据库连接。我可以从任何我喜欢的地方连接到 Postgresql。在主机上,我可以使用 127.0.0.1 地址和 192.X.X.X 地址从 Python 连接到它。但是 Apache Superset 容器无法建立到这个 Postgres 实例的连接——我尝试了 127.0.0.1 和 192.X.X.X——它们都没有工作。
当我进入超集 Docker 容器外壳时,我可以检查所有相关的 IP 地址:
superset@2cf0e6dde567:/app$ ip addr show eth0
48: eth0@if49: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
link/ether 02:42:ac:12:00:06 brd ff:ff:ff:ff:ff:ff link-netnsid 0
inet 172.18.0.6/16 brd 172.18.255.255 scope global eth0
valid_lft forever preferred_lft forever
所以,我的超集 Docker 容器似乎有 172.18.0.6 地址。我也检查了这个:
superset@2cf0e6dde567:/app$ route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
default 172.18.0.1 0.0.0.0 UG 0 0 0 eth0
172.18.0.0 0.0.0.0 255.255.0.0 U 0 0 0 eth0
所以,如果我没记错的话,可以使用这个 IP 地址访问我运行 Postgres 的主机地址 - 172.18.0.1。我用ping 查了一下,没问题:
$ ping 172.18.0.1
PING 172.18.0.1 (172.18.0.1) 56(84) bytes of data.
64 bytes from 172.18.0.1: icmp_seq=1 ttl=64 time=0.061 ms
64 bytes from 172.18.0.1: icmp_seq=2 ttl=64 time=0.103 ms
64 bytes from 172.18.0.1: icmp_seq=3 ttl=64 time=0.063 ms
但我仍然无法使用此 IP 地址连接到 Postgres。可能,我需要自己配置 Postgresql,但我不知道怎么做。我的 pg_hba.conf 看起来像这样:
local all all peer
host all all 127.0.0.1/32 md5
host all all ::1/128 ident
host all all 0.0.0.0/0 md5
我的 postgresql.conf 包含:
listen_addresses = '*'
可能,我仍然缺少一些东西。我该如何解决?
PS。
这是我在 Python shell 中的超集容器中尝试的:
import psycopg2
import sqlalchemy as db
#engine = db.create_engine('postgresql://postgres:postgres@192.168.234.137:5433/test')
#engine = db.create_engine('postgresql://postgres:postgres@127.0.0.1:5433/test')
engine = db.create_engine('postgresql://postgres:postgres@172.18.0.1:5433/test')
cnx = engine.connect()
我尝试了所有可能的 ip:192.168.234.137(我的主机 ip)、127.0.0.1 和 172.18.0.1。它们都不起作用。我刚刚收到来自 Python 的错误消息:
No route to host
Is the server running on host "X.X.X.X" and accepting
TCP/IP connections on port 5433?
其中 X.X.X.X 是 192.168.234.137、127.0.0.1 和 172.18.0.1 之一
【问题讨论】:
-
我知道这个线程 - stackoverflow.com/questions/24319662/… 。但是如你所见,172.X.X.X 地址无法从 Docker 访问,但仍然无法建立数据库连接
-
如果我简单地将
network_mode: host添加到官方 docker-compose.yml 中,它会导致一切崩溃。所以,我宁愿解决这个问题,而不调整存储库中的任何代码。 -
根据您链接的 docker-compose.yml,它在 localhost 上进行侦听。从同一个主机,你能在主机 127.0.0.1 的 5432 端口上连接它吗?
-
@罗伯特·西曼。在我的主机上,Postgres 在端口 5433 上运行,以免与 Apache Superset 自己的 Postgres 容器发生冲突。而且我无法使用主机 127.0.0.1,端口 5433 建立与我的 Postgres 实例的连接。我将附上 serevral 屏幕截图
-
你是如何配置 postgres 在 5433 上运行的?你没有使用你链接的 docker-compose.yml 吗?
标签: postgresql docker docker-compose apache-superset