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



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

dock组合文件:

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 } }

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

try:

...
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。错误消息的行(

最新更新