node.js - 未处理的拒绝 SequelizeConnectionRefusedError:将 ECONNREFUSED 127.0.0.1:5432 与 docker 连接
问题描述
我正在尝试为我的 Postgres 构建一个 docker-compose 文件,该文件使用 sequelize 作为 ORM ( https://sequelize.org/ )。这是我的 docker-compose.yml
version: "2"
services:
web:
build:
context : .
dockerfile: Dockerfile
image : testorganization
ports:
- "5000:5000"
links:
- db
db:
image: postgres
expose:
- 5432
volumes:
- ./data/db:/data/db
environment:
POSTGRES_HOST: db
POSTGRES_DB: postgres
POSTGRES_USER: postgres
POSTGRES_PASS: admin
Dockerfile
FROM node:8-alpine
LABEL description="Docker file for organizations app"
RUN mkdir -p /var/www/logs/
RUN mkdir -p /logs/
RUN mkdir -p /usr/src/
WORKDIR /usr/src/
ADD package.json /usr/src/package.json
RUN npm install --production
COPY . .
CMD ["node", "index.js"]
下面是我的连接字符串
this.sequelize = new Sequelize( 'postgres','postgres','admin',{'host':'db', 'dialect': 'postgres',"define": { "createdAt": "createdat","updatedAt": "updatedat"}, 'operatorAliases': operatorsAliases} );
无论我做什么更改,它总是连接到 127.0.0.1:5432 并得到以下错误:
web_1 | Unhandled rejection SequelizeConnectionRefusedError: connect ECONNREFUSED 127.0.0.1:5432
web_1 | at connection.connect.err (/usr/src/node_modules/sequelize/lib/dialects/postgres/connectionmanager.js:170:24)
web_1 | at Connection.connectingErrorHandler (/usr/src/node_modules/pg/lib/client.js:191:14)
web_1 | at emitOne (events.js:116:13)
web_1 | at Connection.emit (events.js:211:7)
web_1 | at Socket.reportStreamError (/usr/src/node_modules/pg/lib/connection.js:72:10)
web_1 | at emitOne (events.js:116:13)
web_1 | at Socket.emit (events.js:211:7)
web_1 | at emitErrorNT (internal/streams/destroy.js:66:8)
web_1 | at _combinedTickCallback (internal/process/next_tick.js:139:11)
web_1 | at process._tickCallback (internal/process/next_tick.js:181:9)
- 尝试将域保留
db
在连接字符串中,与 docker-compose 文件中使用的引用相同 - 保持所有连接打开 postgres.conf 文件。
我错过了什么?已经坚持了一段时间了。谢谢
解决方案
index.js
const Sequelize = require('sequelize');
const sequelize = new Sequelize("postgres", "postgres", "admin", {
host: "db",
dialect: "postgres",
pool: {
max: 9,
min: 0,
idle: 10000
}
});
sequelize.authenticate().then(() => {
console.log("Success!");
}).catch((err) => {
console.log(err);
});
Dockerfile - 与问题相同
包.json
{
"name": "test",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"sequelize": "",
"pg": ""
}
}
docker-compose.yml(重写版)
重要提示:如果您同时定义图像和 Dockerfile 并构建,则图像优先,并且不会使用您的 dockerfile 构建。
version: "3.2"
services:
web:
build:
context : .
dockerfile: Dockerfile
networks:
- examplenetwork
ports:
- "5000:5000"
depends_on:
- db
db:
image: postgres
networks:
- examplenetwork
volumes:
- postgresql:/var/lib/postgresql
- postgresql_data:/var/lib/postgresql/data
environment:
- POSTGRES_DB=postgres
- POSTGRES_USER=postgres
- POSTGRES_PASS=admin
networks:
examplenetwork:
volumes:
postgresql:
postgresql_data:
推荐阅读
- haskell - 使用 foldr 编写 haskell 函数
- google-api - 为什么我在 google slide API 上读取凭据时出错?
- python - PyQt5,如何使用 QAbstractButton 制作图像切换按钮
- r - geom_bar() 使条形图具有不同的宽度并完全重叠
- python - 我无法从快速应用程序执行 python 脚本
- r - 合并具有部分匹配行的多个数据框
- javascript - 如何修复 Glide.js 上的这个错误?活动幻灯片的宽度始终小于其他幻灯片
- sass - 在 NextJS 中组织 SCSS 文件
- php - 如何使用 apache 和 php 流式传输远程 mp4 视频
- dialogflow-es - DialogFlow - 处理默认回退的最佳实践?