未处理的拒绝SequelizeConnectionRefusedError:将ECONNREFUSED 127.0.0.1:5432与docker连接起来

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了未处理的拒绝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)
  1. 尝试将域db保留在连接字符串中,与docker-compose文件中使用的引用相同
  2. 保持所有连接打开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);
);

在index.js中,您可以找到我使用的引号中的关键区别。请尝试使用双引号。这解决了上面列出的示例代码的问题。

Dockerfile-与问题中的相同

package.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(重写版本)

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:

以上是关于未处理的拒绝SequelizeConnectionRefusedError:将ECONNREFUSED 127.0.0.1:5432与docker连接起来的主要内容,如果未能解决你的问题,请参考以下文章

可能未处理的承诺拒绝未定义不是一个函数

条纹元素抛出“未处理的承诺拒绝”

未处理的拒绝 SequelizeDatabaseError: 未选择数据库

处理快递申请中未处理的承诺拒绝

反应本机中未处理的承诺拒绝错误?

未处理的拒绝 SequelizeEagerLoadingError:未关联,并且无法读取未定义的属性“getTableName”