【问题标题】:Postgres Docker - unable to connect from remote serverPostgres Docker - 无法从远程服务器连接
【发布时间】:2016-08-11 05:06:31
【问题描述】:

我正在使用 postgres:9.5.3 docker 映像。我正在启动容器,然后尝试从远程主机连接到 psql 数据库,但每次都失败并出现错误:

psql: could not connect to server: Connection refused
Is the server running on host "172.18.0.2" and accepting
TCP/IP connections on port 5432?

在我的 docker-compose 文件中,我正在挂载 pg_hba.conf。这是我的 docker-compose 文件:

services:
    db:
      networks:
        - test
      image: postgres:9.5.3
      expose:
        - 5432
      volumes:
        - ./pgdata/:/var/lib/postgresql/data
        - ./pg_hba.conf/:/var/lib/postgresql/data/pg_hba.conf

我已根据here 的说明修改了我的 pg_hba.conf 文件以接受来自所有主机的远程连接。我的pg_hba.conf如下:

# TYPE  DATABASE        USER            ADDRESS                 METHOD

# "local" is for Unix domain socket connections only
local   all             all                                     trust
# IPv4 local connections:
host    all             all             0.0.0.0/0               trust
# IPv6 local connections:
host    all             all             ::0/0                   trust
# Allow replication connections from localhost, by a user with the
# replication privilege.
#local   replication     postgres                                trust
#host    replication     postgres        127.0.0.1/32            trust
#host    replication     postgres        ::1/128                 trust

host all all 0.0.0.0/0 md5

我的 postgresql.conf 也有以下行:listen_addresses = '*'

当我尝试从运行容器的主机连接到数据库时,它连接成功。但是当我尝试使用命令psql -h 172.18.0.2 -U postgres -d postgres -p 5432 从任何远程计算机连接时,它给了我连接错误,这意味着远程连接不起作用。通过所有这些设置,我希望它能够连接。我在这里错过了什么?

【问题讨论】:

  • "172.18.0.2" 172.0.0.0/8 是桥接/路由/防火墙吗? IIRC 172. 网络有一些特别之处。

标签: postgresql docker psql


【解决方案1】:

看起来你实际上并没有发布暴露的端口。

代替:

expose:
  - 5432

使用:

ports:
 - "5432:5432"

expose 文档:

公开端口而不将它们发布到主机 - 它们会 只能被链接的服务访问。只有内部端口可以 指定。

【讨论】:

    猜你喜欢
    • 2019-08-11
    • 1970-01-01
    • 2015-08-26
    • 2019-05-26
    • 1970-01-01
    • 1970-01-01
    • 2020-01-16
    • 2020-06-13
    • 1970-01-01
    相关资源
    最近更新 更多