Node.js 未处理的拒绝SequelizeConnectionRefusedError:将EconRefuse127.0.0.1:5432与dockers连接

Node.js 未处理的拒绝SequelizeConnectionRefusedError:将EconRefuse127.0.0.1:5432与dockers连接,node.js,postgresql,docker,docker-compose,sequelize.js,Node.js,Postgresql,Docker,Docker Compose,Sequelize.js,我正在尝试为我的Postgres构建一个docker compose文件,它使用sequelize作为ORM()。这是我的docker-compose.yml version: "2" services: web: build: context : . dockerfile: Dockerfile image : testorganization ports: - "5000:5000" links: - db db: image: p

我正在尝试为我的Postgres构建一个docker compose文件,它使用sequelize作为ORM()。这是我的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-与问题中相同

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

    重要提示:如果您同时定义映像和Dockerfile以及build,则映像具有优先权,并且不会使用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:
    

    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-与问题中相同

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

    重要提示:如果您同时定义映像和Dockerfile以及build,则映像具有优先权,并且不会使用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:
    

    您能否共享Dockerfile/source的最低版本以再现错误?请发布具体的错误消息。您能否共享一个示例代码来重现错误?目前,它指出了代码中的一个问题。在基础设施方面,容器之间的通信对我来说是有效的,上面的docker-compose.ymledite带有错误消息。过帐代码将太长,因此过帐连接字符串。如果我在主机中进行更改并直接运行应用程序,它会抛出一个正确的错误,称为ADDRESS not found。还有,在代码中是否需要使用任何环境变量?这就是为什么我要求使用示例代码。e、 g.仅db连接;)此外,package.json有助于了解您的依赖项。您可以共享Dockerfile/source的最低版本来重现错误吗?请发布具体的错误消息。您能否共享一个示例代码来重现错误?目前,它指出了代码中的一个问题。在基础设施方面,容器之间的通信对我来说是有效的,上面的docker-compose.ymledite带有错误消息。过帐代码将太长,因此过帐连接字符串。如果我在主机中进行更改并直接运行应用程序,它会抛出一个正确的错误,称为ADDRESS not found。还有,在代码中是否需要使用任何环境变量?这就是为什么我要求使用示例代码。e、 g.仅db连接;)此外,package.json有助于了解您的从属关系。这一行
    image:testorganization
    需要删除。看起来Docker将
    db
    DNS名称解析为
    localhost
    ,因为您在Docker compose文件中使用了旧的
    链接
    语法。我建议不要再使用
    链接
    ,而是在docker compose文件中定义一个网络。您可以阅读更多内容,需要删除这一行
    image:testorganization
    。Docker似乎将
    db
    DNS名称解析为
    localhost
    ,因为您在Docker compose文件中使用了旧的
    链接
    语法。我建议不要再使用
    链接
    ,而是在docker compose文件中定义一个网络。你可以读更多的书