【问题标题】:Dockerizing PostgreSQL - psql Connection refusedDockerizing PostgreSQL - psql 连接被拒绝
【发布时间】:2014-10-13 15:08:42
【问题描述】:

我正在使用 Docker,我想 Dockerize 一个 Postgres 容器。

我在关注官方example,但是无法连接到使用psql运行的镜像。

我使用示例的内容创建了 Dockerfile。我从 Dockerfile 构建了一个图像并为其分配了一个名称。然后我运行 PostgreSQL 服务器容器(在前台)。

~/test » docker run --rm -P --name pg_test eg_postgresql                                                                                                       
2014-10-10 06:12:43 UTC LOG:  database system was interrupted; last known up at 2014-10-10 06:12:29 UTC
2014-10-10 06:12:43 UTC LOG:  database system was not properly shut down; automatic recovery in progress
2014-10-10 06:12:43 UTC LOG:  redo starts at 0/1782F68
2014-10-10 06:12:43 UTC LOG:  record with zero length at 0/1782FA8
2014-10-10 06:12:43 UTC LOG:  redo done at 0/1782F68
2014-10-10 06:12:43 UTC LOG:  last completed transaction was at log time 2014-10-10 06:12:29.2487+00
2014-10-10 06:12:43 UTC LOG:  database system is ready to accept connections
2014-10-10 06:12:43 UTC LOG:  autovacuum launcher started

然后我打开另一个终端找出端口:

~/test » docker ps                                                                                                                                             
CONTAINER ID        IMAGE                  COMMAND                CREATED             STATUS              PORTS                     NAMES
aaedb0479139        eg_postgresql:latest   "/usr/lib/postgresql   3 days ago          Up 41 seconds       0.0.0.0:49154->5432/tcp   pg_test

所以我可以使用 psql 连接到实例。但我不能...

~/test » psql -h localhost -p 49154 -d docker -U docker --password                                                                                             
Password for user docker:
psql: could not connect to server: Connection refused
    Is the server running on host "localhost" (127.0.0.1) and accepting
    TCP/IP connections on port 49154?
could not connect to server: Connection refused
    Is the server running on host "localhost" (::1) and accepting
    TCP/IP connections on port 49154?
could not connect to server: Connection refused
    Is the server running on host "localhost" (fe80::1) and accepting
    TCP/IP connections on port 49154?

感谢任何帮助。

【问题讨论】:

  • 是否使用容器链接'?
  • 是的,确实如此。但我希望能够从我的主机系统进行连接。
  • netstat | grep 49154 说什么?
  • 如果你使用 5433:5432 它不会暴露 5433 端口,你会得到连接被拒绝。只要将其更改为“5433:5432”,它就会起作用。用官方的 postgres:13.2 镜像测试(也有 13 个)。

标签: postgresql docker psql


【解决方案1】:

对我来说,解决方案只是在您的连接字符串中使用 host.docker.internal 而不是 localhost

https://github.com/npgsql/Npgsql.EntityFrameworkCore.PostgreSQL/issues/225

【讨论】:

  • 这对我有用。 (注意:我也在 docker 上运行 pgadmin)
  • 也为我工作过,上次我做同样的设置时我不需要它......我不知道为什么。不过,我很高兴它成功了!
  • 为我工作,在 Windows 上使用 WSL 运行 docker
  • 也为我工作。谢谢
【解决方案2】:

如果将 --publish 选项添加到 docker run 命令中

docker run --rm -P --publish 127.0.0.1:5432:5432 --name pg_test eg_postgresql 

当您运行 docker 文件时,以下内容将起作用(注意端口现在是 5432)

psql -h localhost -p 5432 -d docker -U docker --password

【讨论】:

  • 仍在发生,看来发布端口并不能解决问题。
  • 您是否有防火墙阻止您连接到该端口?
【解决方案3】:

在我的 Mac 上运行它对我有用:

 $ boot2docker ip
 The VM's Host only interface IP address is: 192.168.59.103

然后按照以下方式连接:

 $ psql -h 192.168.59.103 -p 49159 -d docker -U docker --password

这并不理想,但https://docs.docker.com/installation/mac/ 的说明表明,如果您想直接从您的 mac 连接,这是正确的解决方案。

【讨论】:

  • 这可能是网络配置问题吗?我知道设置网络以将我的容器暴露给本地主机的环境并不容易。我的案例是 VirtualBox 运行 centos7 来宾。我需要向虚拟盒网络配置添加第二个接口,并使用仅主机模式在第二个子网上加入主机和来宾。然后我使用 squid 在网络之间进行反向代理。可能有一种方法可以用路由做到这一点,但这似乎工作正常。
【解决方案4】:

要获取正在运行的容器的 IP 地址,您可以:

docker inspect pg_test | grep IPAddress

然后用它代替'localhost'。

其中 pg_test 是从那里收到的容器名称

docker ps

【讨论】:

    【解决方案5】:

    有人回答了这个问题here

    总结:仅仅发布容器的端口是不够的,因为在 OS x 上,docker 容器是在虚拟机上运行的。 要检索虚拟机的“真实”地址,您应该使用管理 docker 守护进程和容器的工具。

    我建议使用docker-machine

    逻辑与 wf answer 非常相似,但不是使用 boot2docker,而是使用 docker-machine。

    1-/获取虚拟机的名字

    docker-machine ls
    

    2-/ 使用虚拟机的名称(例如,默认)检索虚拟机的 IP 地址

    docker-machine ip default
    

    3-/ 在需要指定主机值时使用此值。

    【讨论】:

      猜你喜欢
      • 2013-05-19
      • 2017-08-05
      • 1970-01-01
      • 2012-06-05
      • 1970-01-01
      • 1970-01-01
      • 2019-08-23
      • 2013-05-30
      • 2017-08-08
      相关资源
      最近更新 更多