SequelizeConnectionRefusedError:使用docker使用sequelize时连接ECONNREFUSED 127.0.0.1:5432

时间:2020-08-07 15:50:29

标签: postgresql docker docker-compose

使用sequelize作为orm后端对节点postgres数据库进行泊坞时,出现以下错误

未处理的拒绝SequelizeConnectionRefusedError:连接 ECONNREFUSED 127.0.0.1:5432 app_1 |在 connection.connect.err (/home/app/node_modules/sequelize/lib/dialects/postgres/connection-manager.js:170:24)

这些代码行似乎是罪魁祸首,docker不应连接这些凭据,因为这是我的本地凭据。

if (process.env.NODE_ENV === "production") {
  var sequelize = new Sequelize(process.env.DATABASE_URL);
} else {
  // docker is looking at these credentials..... when it should not
  var sequelize = new Sequelize("elifullstack", "eli", "", {
    host: "127.0.0.1",
    dialect: "postgres",
    pool: {
      max: 100,
      min: 0,
      idle: 200000,
      // @note https://github.com/sequelize/sequelize/issues/8133#issuecomment-359993057
      acquire: 1000000,
    },
  });
}

docker-compose.yml

# docker-compose.yml
version: "3"
services:
  app:
    build: ./server
    depends_on:
      - database
    ports:
      - 5000:5000
    environment:
      # database refers to the database server at the bottom called "database"
      - PSQL_HOST=database
      - PSQL_USER=postgres
      - PORT=5000
      - PSQL_NAME=elitypescript
    command: npm run server
  client:
    build: ./client
    image: react_client
    links:
      - app
    working_dir: /home/node/app/client
    volumes:
      - ./:/home/node/app
    ports:
      - 3001:3001
    command: npm run start
    env_file:
      - ./client/.env

  database:
    image: postgres:9.6.8-alpine
    volumes:
      - database:/var/lib/postgresql/data
    ports:
      - 3030:5432

volumes:
  database:

./ server / dockerFile

FROM node:10.6.0
COPY . /home/app
WORKDIR /home/app
COPY package.json ./
RUN npm install
EXPOSE 5000

我查看了类似以下的其他问题,但最终并没有帮助解决问题。

Docker - SequelizeConnectionRefusedError: connect ECONNREFUSED 127.0.0.1:3306

SequelizeConnectionRefusedError: connect ECONNREFUSED 127.0.0.1:3306

1 个答案:

答案 0 :(得分:0)

我解决了...

我所做的就是改变这一点

 host: "127.0.0.1",

对此

let sequelize;
if (process.env.NODE_ENV === "production") {
  sequelize = new Sequelize(process.env.DATABASE_URL);
} else {
  sequelize = new Sequelize(
    process.env.POSTGRES_DB || "elitypescript",
    process.env.POSTGRES_USER || "eli",
    "",
    {
      host: process.env.PSQL_HOST || "localhost",
      dialect: "postgres",
      pool: {
        max: 100,
        min: 0,
        idle: 200000,
        // @note https://github.com/sequelize/sequelize/issues/8133#issuecomment-359993057
        acquire: 1000000,
      },
    }
  );
}

那样,主机将被设置为这样的docker环境变量

PSQL_HOST: database

并且连接到

database:
    image: postgres:9.6.8-alpine
    volumes:
      - database:/var/lib/postgresql/data
    ports:
      - 3030:5432

修改

# docker-compose.yml
version: "3"
services:
  app:
    build: ./server
    depends_on:
      - database
    ports:
      - 5000:5000
    environment:
      PSQL_HOST: database
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-password}
      POSTGRES_USER: ${POSTGRES_USER:-postgres}
      POSTGRES_DB: ${POSTGRES_DB:-elitypescript}
    command: npm run server
  client:
    build: ./client
    image: react_client
    links:
      - app
    working_dir: /home/node/app/client
    volumes:
      - ./:/home/node/app
    ports:
      - 3001:3001
    command: npm run start
    env_file:
      - ./client/.env

  database:
    image: postgres:9.6.8-alpine
    volumes:
      - database:/var/lib/postgresql/data
    ports:
      - 3030:5432

volumes:
  database: