【问题标题】:Access host postgres from docker container从 docker 容器访问主机 postgres
【发布时间】:2020-02-17 06:22:58
【问题描述】:

我有一个 CentOS 7 服务器,在其上分别运行 postgres(端口 5432)和 docker。

目前我正在尝试从我的 docker 容器访问 postgres 数据库。但我收到以下错误:

    at Connection.parseE (/usr/app/node_modules/pg/lib/connection.js:604:11)
    at Connection.parseMessage (/usr/app/node_modules/pg/lib/connection.js:401:19)
    at Socket.<anonymous> (/usr/app/node_modules/pg/lib/connection.js:121:22)
    at Socket.emit (events.js:198:13)
    at addChunk (_stream_readable.js:288:12)
    at readableAddChunk (_stream_readable.js:269:11)
    at Socket.Readable.push (_stream_readable.js:224:10)
    at TCP.onStreamRead [as onread] (internal/stream_base_commons.js:94:17)
  name: 'error',
  length: 161,
  severity: 'FATAL',
  code: '28000',
  detail: undefined,
  hint: undefined,
  position: undefined,
  internalPosition: undefined,
  internalQuery: undefined,
  where: undefined,
  schema: undefined,
  table: undefined,
  column: undefined,
  dataType: undefined,
  constraint: undefined,
  file: 'auth.c',
  line: '489',
  routine: 'ClientAuthentication' }```

I have change pg_hba.conf of my host postgres application that too didn't solve the issue.
Any help will be appreciated.

【问题讨论】:

  • 请分享更多信息:您的 postgres 服务器绑定到哪个端口/IP? postgres 是否也在容器中运行?你能分享一个简单的客户场景吗?例如用于构建无法连接到 postrges 的客户端的 Dockerfile 以及用于启动 docker 容器的命令
  • 感谢您的回复。 1)我的 postgres 在主机服务器的端口 5432 上运行。 2)Docker也在同一主机上配置。 3) 任何 docker 容器上都没有运行 postgres。 4) Docker 构建成功。 5)运行镜像的命令:docker run -d -p 3001:3001
  • 感谢您的澄清。您是否尝试使用主机的 IP 访问 postgres 服务器? Postgres 服务器是否绑定到主机的 IP? (例如不是 127.0.0.1 / localhost)
  • 是的,我正在使用主机的IP进行连接。
  • 你的 CentOS 7 机器上运行防火墙吗?例如防火墙?如果是这样,您的防火墙配置中是否打开了 postgres 端口?

标签: postgresql docker centos centos7


【解决方案1】:

感谢 Clemans 的帮助,我找到了解决方案。问题是由于 docker 网络,因为它无法连接到主机。因为默认情况下我的容器在 docker 网桥中运行。我将其更改为遇到解决我的问题的主机。下面是我用来运行我的容器的命令 docker run --network=host -d &lt;image name&gt;

【讨论】:

  • 太棒了!感谢您发布问题的解决方案:)
猜你喜欢
  • 2017-02-18
  • 1970-01-01
  • 1970-01-01
  • 2018-08-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-30
相关资源
最近更新 更多