【问题标题】:ECONNREFUSED error in docker-compose with NodeJS and postgresql in google cloud使用谷歌云中的 NodeJS 和 postgresql 在 docker-compose 中出现 ECONNREFUSED 错误
【发布时间】:2019-02-19 22:49:14
【问题描述】:

我已经使用 Node.js 和 postgresql 创建了我的 react 应用程序,并部署在谷歌云中。我创建了一个 postgres 和 nodejs 的 docker 图像,并将图像上传到 docker hub。从 gcloud 我访问那些图像。

这是我的 docker-compose-production.yml 文件。

version: '2.0'
services:
  postgres:
    image : mycompany/myapp:pglatest
    restart : always
    volumes:
      - ./backdata/databackups:/var/lib/postgresql/backdata
    ports:
      - "5433:5432"

  backend:
    image: mycompany/myapp:nodelatest7
    command: npm run start
    ports:
      - "5001:5000"
    depends_on:
      - postgres
    environment:
      POSTGRES_URL: postgresql://postgres:root@postgres:5432/db_mydb 
      DEBUG: hms-backend:*

当我运行命令时

sudo docker-compose -f docker-compose-production.yml up --build -d

已创建 2 张图像。

之后我运行了tail命令

sudo docker-compose -f docker-compose-production.yml logs -t backend

我遇到了错误

后端_1 | 2018-09-15T09:12:18.926001351Z REST API 监听端口 5000
后端_1 | 2018-09-15T09:12:18.937246598Z 错误 { 错误:连接 ECONNREFUSED 192.168.80.2:5432
后端_1 | 2018-09-15T09:12:18.937268668Z 在 TCPConnectWrap.afterConnect [as oncomplete] (net.js:1191:14)
后端_1 | 2018-09-15T09:12:18.937280934Z 错误号:'ECONNREFUSED',
后端_1 | 2018-09-15T09:12:18.937283960Z 代码:'ECONNREFUSED',
后端_1 | 2018-09-15T09:12:18.937286817Z 系统调用:“连接”,
后端_1 | 2018-09-15T09:12:18.937289488Z 地址:'192.168.80.2',
后端_1 | 2018-09-15T09:12:18.937292260Z 端口:5432 }

如何解决这个问题

【问题讨论】:

  • 如果 postgres 配置为侦听所有接口 (*) 而不是默认的localhost,您应该检查 postgresql.conf
  • 感谢您的回复。我试过了,但没有解决我的问题
  • 你确定你的mycompany/myapp:pglatest暴露了5432端口吗?它假设在该图像的 Dockerfile 中有EXPOSE 5432

标签: node.js postgresql docker docker-compose econnrefused


【解决方案1】:

对我来说,你的 postgres 网址是错误的:postgresql://postgres:root@postgres:5432/db_mydb

应该是 postgresql://postgres:root@postgres:5433/db_mydb 因为 postgres “暴露”端口在 5433

嗯,我认为你应该在 docker-compose 中添加“container_name”

services:
  postgres:
     container_name: my_postgres  

并使用此名称作为您的 postgres 的“地址”

postgresql://my_postgres:root@postgres:5433/db_mydb

【讨论】:

  • 感谢您的回复。这并没有解决我的问题,我遇到了同样的错误
  • 以防万一,新配置被 docker 使用了?所以你得到了你的日志“端口 5433”?
  • 我编辑了答案,你用 container_name 试过了吗?
  • 我已经改变了,但我遇到了同样的问题。谢谢你的回复。
猜你喜欢
  • 2021-02-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-03
  • 2020-02-03
  • 2018-01-16
  • 2022-12-21
  • 2020-05-02
相关资源
最近更新 更多