当前位置: 首页 > 知识库问答 >
问题:

从docker容器中连接到localhostpostgres数据库[副本]

劳麒
2023-03-14

我有一个linux postgres数据库,在我的本地主机端口5432上运行。我的机器网卡的IP是192.168.68.80。我的postgres配置文件如下所示:

listen_addresses = '*'
host    all             all             0.0.0.0/0               md5
host    all             all             ::/0                    md5

现在,我想从docker容器中运行的节点应用程序连接到这个数据库。这是我的环境变量文件加载到节点时的样子:

PGHOST=localhost // Host of the postgres db server
PGPORT=5432 // Port of the postgres db

完成此设置后,我使用docker文件构建docker映像。然后,我尝试在linux上使用以下命令将dockerfile作为容器运行:

sudo docker run --network="host" --env-file .env -p 3000:3000 repo:tag

,应用开始运行,并侦听端口 3000 上的连接。但是,每当我尝试连接到该应用程序时,该应用程序都无法连接到postgres数据库服务器,并给出以下错误:

/node_modules/sequelize/lib/dialects/postgres/connection-manager.js:172
                reject(new sequelizeErrors.ConnectionRefusedError(err));
                       ^

ConnectionRefusedError [SequelizeConnectionRefusedError]: connect ECONNREFUSED 127.0.0.1:5432
    at Client._connectionCallback (/node_modules/sequelize/lib/dialects/postgres/connection-manager.js:172:24)
    at Client._handleErrorWhileConnecting (/node_modules/pg/lib/client.js:305:19)
    at Client._handleErrorEvent (/node_modules/pg/lib/client.js:315:19)
    at Connection.emit (node:events:394:28)
    at Socket.reportStreamError (/node_modules/pg/lib/connection.js:52:12)
    at Socket.emit (node:events:394:28)
    at emitErrorNT (node:internal/streams/destroy:193:8)
    at emitErrorCloseNT (node:internal/streams/destroy:158:3)
    at processTicksAndRejections (node:internal/process/task_queues:83:21) {
  parent: Error: connect ECONNREFUSED 127.0.0.1:5432
      at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1133:16) {
    errno: -111,
    code: 'ECONNREFUSED',
    syscall: 'connect',
    address: '127.0.0.1',
    port: 5432
  },
  original: Error: connect ECONNREFUSED 127.0.0.1:5432
      at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1133:16) {
    errno: -111,
    code: 'ECONNREFUSED',
    syscall: 'connect',
    address: '127.0.0.1',
    port: 5432
  }
}

我不知道为什么连接被拒绝,因为它似乎应该能够连接。

我当前的ip表,以防有帮助:

user$ sudo iptables --list
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         

Chain FORWARD (policy DROP)
target     prot opt source               destination         
DOCKER-USER  all  --  anywhere             anywhere            
DOCKER-ISOLATION-STAGE-1  all  --  anywhere             anywhere            
ACCEPT     all  --  anywhere             anywhere             ctstate RELATED,ESTABLISHED
DOCKER     all  --  anywhere             anywhere            
ACCEPT     all  --  anywhere             anywhere            
ACCEPT     all  --  anywhere             anywhere            
ACCEPT     all  --  anywhere             anywhere             ctstate RELATED,ESTABLISHED
DOCKER     all  --  anywhere             anywhere            
ACCEPT     all  --  anywhere             anywhere            
ACCEPT     all  --  anywhere             anywhere            

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination         

Chain DOCKER (2 references)
target     prot opt source               destination         

Chain DOCKER-ISOLATION-STAGE-1 (1 references)
target     prot opt source               destination         
DOCKER-ISOLATION-STAGE-2  all  --  anywhere             anywhere            
DOCKER-ISOLATION-STAGE-2  all  --  anywhere             anywhere            
RETURN     all  --  anywhere             anywhere            

Chain DOCKER-ISOLATION-STAGE-2 (2 references)
target     prot opt source               destination         
DROP       all  --  anywhere             anywhere            
DROP       all  --  anywhere             anywhere            
RETURN     all  --  anywhere             anywhere            

Chain DOCKER-USER (1 references)
target     prot opt source               destination         
RETURN     all  --  anywhere             anywhere

共有2个答案

邓德厚
2023-03-14

docker 在容器中有自己的网络。当你说“PGHOST=localhost”时,它将连接到 docker 容器的内部 localhost(与你的计算机 localhost 无关),而不是你的计算机 localhost。你必须放置一个运行你的 docker 和机器的同一网络。它可以是您的网络 IP。您可以通过在linux中给出命令“ip a”来获取IP。在我的情况下,我的wifi是公共网络,IP是“192.168.X.X/24”,给出“192.168.X.X”而不是“localhost”将适用于我。

许奇
2023-03-14

容器中,主机的IP地址172.17.0.1。因此您应该连接到172.17.0.1:5432

 类似资料:
  • 在将Django docker容器连接到Postgres数据库时,我收到以下错误。 下面是运行容器的Dockerfile pg_hba.conf->宿主all all 0.0.0.0/0 md5 我已经读到上面的以下细节打开数据库上的连接。

  • 我正在处理dockerize我的本地服务器,在那里有数据库,应用程序服务器和所有额外的配置来部署我的应用程序。目前,我已经创建了3个docker容器,运行像Tomcat或WildFly这样的应用程序服务器,并公开了部署的应用程序的端口,这些端口Java并从/读写数据。到我本地的数据库。在每个应用程序的文件中,我已经通过在我的实际配置中配置了访问权限,没有docker也可以。 我想将容器上的应用程序

  • 在我的项目中,当我运行后端时,使用Quarkus、Angular和PostgreSQL DB 这是我放在后端文件夹(Quarkus)中的“docker-compose.yml”文件: 但是当我构建一个Quarkus泊坞窗映像并尝试在泊坞窗容器中运行它时,它失败了!知道角度泊坞站容器运行良好,数据库也是如此。以下是我在运行容器后获得的错误日志: 使用 /opt/jboss/容器/java/运行/运行

  • 我是debezium的新手,我正在尝试将我的kafka连接器连接到我本地WAMP服务器上已经存在的MySQL数据库。 我用我的邮递员发了以下邮件 我的不良反应

  • 问题内容: 我最近一直在研究Docker和QGIS,并按照本教程中的说明安装了一个容器。 尽管我无法连接到包含我的所有GIS数据的localhost postgres数据库,但一切工作都很好。我认为这是因为我的postgres数据库未配置为接受远程连接,并且已经按照本文中的说明编辑了postgres conf文件以允许远程连接。 尝试连接到在Docker中运行QGIS的数据库时,我仍然收到错误消息

  • 我最近一直在玩Docker和QGIS,并按照本教程中的说明安装了一个容器。 我运行的是Ubuntu 14.04;Postgres 9.3