SequelizeConnectionRefusedError:connect ECONREFUSED 127.0.0.1:5432 when using docker to use sequelize

rryofs0p  于 2023-03-22  发布在  Docker
关注(0)|答案(1)|浏览(130)

我在使用sequelize作为orm后端对node postgres数据库进行dockerizing时出现以下错误
未处理的拒绝SequelizeConnectionRefusedError:connect ECONNREFUSED 127.0.0.1:5432 app_1|at 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,
    },
  });
}

停靠-编写.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:

./服务器/停靠文件

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

kuhbmx9i

kuhbmx9i1#

我解决了...
我所做的就是改变这个

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:

更新日期:

上述解决方案可能不适用于遇到此问题的大多数人。
但是有些事情需要考虑
https://stackoverflow.com/a/33363660
变更:
127.0.0.1 到CONTAINER_NAME(例如数据库)

相关问题