docker-compose POSTGRES&SEQUELIZE导致数据库连接错误

时间:2018-11-09 18:33:42

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

我有一个使用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 } }

2 个答案:

答案 0 :(得分:0)

尝试:

...
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.行)。

答案 1 :(得分:0)

将第一个更改更改为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