[英]Can't connect to Postgres Docker container remotely
I have a docker container running on a centos machine using the official Postgres docker image and it cannot be connected remotely.我有一个使用官方 Postgres docker 映像在 centos 机器上运行的 docker 容器,但它无法远程连接。
sudo docker ps | grep postgres
sudo docker ps | grep postgres
gives sudo docker ps | grep postgres
给出
492192ff9170 postgres "/docker-entrypoint.s" 2 years ago Up 18 hours 0.0.0.0:5432->5432/tcp postgres
492192ff9170 postgres "/docker-entrypoint.s" 2 年前 上升 18 小时 0.0.0.0:5432->5432/tcp postgres
when I try psql -h <server domain name> -p 5432 -U postgres
on the other machine, it just printed out当我在另一台机器上尝试
psql -h <server domain name> -p 5432 -U postgres
时,它只是打印出来
psql: could not connect to server: Operation timed out
psql:无法连接到服务器:操作超时
Is the server running on host [domain name] ([ip address]) and accepting TCP/IP connections on port 5432?服务器是否在主机 [域名]([ip 地址])上运行并接受端口 5432 上的 TCP/IP 连接?
I did sudo iptables -S | grep 5432
我做了
sudo iptables -S | grep 5432
sudo iptables -S | grep 5432
on the remote server and it printed out远程服务器上的
sudo iptables -S | grep 5432
并打印出来
-A DOCKER -d 172.17.0.2/32 !
-一个码头工人 -d 172.17.0.2/32 ! -i docker0 -o docker0 -p tcp -m tcp --dport 5432 -j ACCEPT
-i docker0 -o docker0 -p tcp -m tcp --dport 5432 -j 接受
so it doesn't looks like port 5432
was blocked所以看起来
port 5432
被阻止
Inside the container /var/lib/postgresql/data/
,在容器
/var/lib/postgresql/data/
,
host all all 0.0.0.0/0 md5
had been added to the pg_hba.conf
, while postgresql.conf
has listen_addresses = '*'
specified. host all all 0.0.0.0/0 md5
已添加到pg_hba.conf
,而postgresql.conf
已指定listen_addresses = '*'
。
Still, it cannot be accessed remotely, although I can successfully connect to the instance locally using psql -h localhost -p 5432 -U postgres
尽管如此,它仍然无法远程访问,尽管我可以使用
psql -h localhost -p 5432 -U postgres
在本地成功连接到实例
Could any one shed some lights on this issue?任何人都可以对这个问题有所了解吗? Much appreciated.
非常感激。
How did you started the postgres container? 你是怎么开始使用postgres容器的?
Probably you're only missing a container-to-host port binding option. 可能你只是缺少容器到主机端口绑定选项。
Try adding -p 5432:5432
to your docker run command. 尝试将
-p 5432:5432
添加到docker run命令。 This will map port 5432 in the container to port 5432 on the Docker host (your remote server). 这会将容器中的端口5432映射到Docker主机(远程服务器)上的端口5432。
See docker networking docs for more information. 有关更多信息,请参阅docker网络文档 。 See also this postgres example .
另见这个postgres示例 。
I encountered a similar problem, I was not able to connect to a postgres db in a docker container remotely.我遇到了类似的问题,我无法远程连接到 docker 容器中的 postgres 数据库。 I knocked my head around for 2 days until I discovered the solution.
我敲了敲脑袋 2 天,直到我找到了解决方案。
I configured my ports in the following way: -p 5438:5432
.我按以下方式配置了我的端口:
-p 5438:5432
。 In other words, I was trying to expose port 5438 on my host.换句话说,我试图在我的主机上公开端口 5438。
My host, as it turns out, was a Azure VM with Ubuntu installed.事实证明,我的主机是一台安装了 Ubuntu 的 Azure VM。 And I finally remembered that under the "Networking" tab in the Azure portal you have a list of "Inbound port rules."
我终于记得在 Azure 门户的“网络”选项卡下有一个“入站端口规则”列表。
Well, I needed to open port 5438. I did that and BANG it started working.好吧,我需要打开端口 5438。我这样做了,然后它就开始工作了。 Wasn't an issue with docker config at all.
根本不是 docker config 的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.