【问题标题】:Database connection errors with docker-compose POSTGRES & SEQUELIZEdocker-compose POSTGRES & SEQUELIZE 的数据库连接错误
【发布时间】:2019-04-13 08:45:14
【问题描述】:

我有一个使用 postgres 和 sequelize 的节点应用程序。我有一个将运行我的服务器的 docker 文件。我还有一个 docker compose 文件,它将运行一个 web 图像和 db 图像,它们链接我的 docker 图像并将它们连接在一起。

我能够通过 docker 文件让我的服务器运行。我正在尝试使用 docker-compose 文件来运行数据库并让它们工作。我正在尝试连接的数据库出现连接错误,我不确定此错误来自何处...

Dockerfile

FROM node:10
WORKDIR /app
COPY package.json ./app
RUN npm install
COPY . /app
CMD npm start
EXPOSE 5585

停靠撰写文件:

version: "2"
services: 
  web:
    build: .
    ports: 
      - 80:5585
    command: npm start 
    depends_on:
      - db
    environment:
      - DATABASE_URL=postgres://username:password@db:5432/addidas
  db:
    image: postgres 
    restart: always
    ports:
      - "5432:5432"
    environment: 
      - POSTGRES_USER=username
      - POSTGRES_PASSWORD=password
      - POSTGRES_DB=addidas

我还使用 sequelize 将我的数据库与 express 连接起来。我需要改变任何东西来做到这一点。

错误:

web_1  | Fri, 09 Nov 2018 18:26:47 GMT sequelize deprecated String based operators are now deprecated. Please use Symbol based operators for better security, read more at http://docs.sequelizejs.com/manual/tutorial/querying.html#operators at node_modules/sequelize/lib/sequelize.js:242:13
web_1  | server is running at http://localhost:3001
web_1  | { SequelizeConnectionRefusedError: connect ECONNREFUSED 172.19.0.3:5432
web_1  |     at connection.connect.err (/app/node_modules/sequelize/lib/dialects/postgres/connection-manager.js:116:24)
web_1  |     at Connection.connectingErrorHandler (/app/node_modules/pg/lib/client.js:140:14)
web_1  |     at Connection.emit (events.js:182:13)
web_1  |     at Socket.reportStreamError (/app/node_modules/pg/lib/connection.js:71:10)
web_1  |     at Socket.emit (events.js:182:13)
web_1  |     at emitErrorNT (internal/streams/destroy.js:82:8)
web_1  |     at emitErrorAndCloseNT (internal/streams/destroy.js:50:3)
web_1  |     at process._tickCallback (internal/process/next_tick.js:63:19)
web_1  |   name: 'SequelizeConnectionRefusedError',
web_1  |   parent:
web_1  |    { Error: connect ECONNREFUSED 172.19.0.3:5432
web_1  |        at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1113:14)
web_1  |      errno: 'ECONNREFUSED',
web_1  |      code: 'ECONNREFUSED',
web_1  |      syscall: 'connect',
web_1  |      address: '172.19.0.3',
web_1  |      port: 5432 },
web_1  |   original:
web_1  |    { Error: connect ECONNREFUSED 172.19.0.3:5432
web_1  |        at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1113:14)
web_1  |      errno: 'ECONNREFUSED',
web_1  |      code: 'ECONNREFUSED',
web_1  |      syscall: 'connect',
web_1  |      address: '172.19.0.3',
web_1  |      port: 5432 } }
web_1  | { SequelizeConnectionRefusedError: connect ECONNREFUSED 172.19.0.3:5432
web_1  |     at connection.connect.err (/app/node_modules/sequelize/lib/dialects/postgres/connection-manager.js:116:24)
web_1  |     at Connection.connectingErrorHandler (/app/node_modules/pg/lib/client.js:140:14)
web_1  |     at Connection.emit (events.js:182:13)
web_1  |     at Socket.reportStreamError (/app/node_modules/pg/lib/connection.js:71:10)
web_1  |     at Socket.emit (events.js:182:13)
web_1  |     at emitErrorNT (internal/streams/destroy.js:82:8)
web_1  |     at emitErrorAndCloseNT (internal/streams/destroy.js:50:3)
web_1  |     at process._tickCallback (internal/process/next_tick.js:63:19)
web_1  |   name: 'SequelizeConnectionRefusedError',
web_1  |   parent:
web_1  |    { Error: connect ECONNREFUSED 172.19.0.3:5432
web_1  |        at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1113:14)
web_1  |      errno: 'ECONNREFUSED',
web_1  |      code: 'ECONNREFUSED',
web_1  |      syscall: 'connect',
web_1  |      address: '172.19.0.3',
web_1  |      port: 5432 },
web_1  |   original:
web_1  |    { Error: connect ECONNREFUSED 172.19.0.3:5432
web_1  |        at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1113:14)
web_1  |      errno: 'ECONNREFUSED',
web_1  |      code: 'ECONNREFUSED',
web_1  |      syscall: 'connect',
web_1  |      address: '172.19.0.3',
web_1  |      port: 5432 } }

【问题讨论】:

  • 哥们,我可以发表你的评论吗?

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


【解决方案1】:

尝试:

...
web:
    build: 
      context: .
    ports: 
      - 80:5585
    command: npm start
    network_mode: service:db 
    links:
      - db
    environment:
      - DATABASE_URL=postgres://username:password@db:5432/addidas
...

另一件事是,从暴露行我猜你想在端口 5585 上运行节点应用程序。但它实际上是在端口 3001 上运行(基于错误消息的第 2 行)

【讨论】:

  • 能贴出nodejs应用的连接代码吗?
【解决方案2】:

第一次更改为 5432 以默认 postgres 端口

db:
image: postgres 
restart: always
ports:
  - "5432:3306" //default postgres port 
environment: 
  - POSTGRES_USER=username
  - POSTGRES_PASSWORD=password
  - POSTGRES_DB=addidas
  1. 允许您要连接的端口

sudo ufw allow 5432

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-28
    • 2020-05-14
    • 2022-01-01
    相关资源
    最近更新 更多