从 docker 容器访问主机 postgres

Access host postgres from docker container

我有一台 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.

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