4

I'm getting the following error when dockerizing a node postgres database using sequelize as an orm backend

Unhandled rejection 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)

These lines of code seems to be the culprit, docker should not be connecting these credentials as this is for my local.

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

I looked at other similar questions like the following, but it ultimately did not help solve the issue.

Docker - SequelizeConnectionRefusedError: connect ECONNREFUSED 127.0.0.1:3306

SequelizeConnectionRefusedError: connect ECONNREFUSED 127.0.0.1:3306

BARNOWL
  • 3,326
  • 10
  • 44
  • 80

3 Answers3

5

I solved it...

What i did was change this

 host: "127.0.0.1",

to this

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,
      },
    }
  );
}

that way the host would be set to docker environment variable like this

PSQL_HOST: database

and that connects to

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

Edit

# 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:

Update:

The above solution may not be applicable to most folks encountering this issue.

However something to take into account

https://stackoverflow.com/a/33363660

Change:

127.0.0.1 to CONTAINER_NAME (e.g. database)

BARNOWL
  • 3,326
  • 10
  • 44
  • 80
0

the issue could be on docker site, as every time running docker-compose each container gets different IP, so to connect to db you need to specify the right IP address. To solve this issue you can add a networking part to the docker-compose.yml and make addresses static.

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Apr 13 '23 at 05:06
0

I used this configuration for docker to work

{
    host:"postgres-db",
    username: 'postgres',
    password: 'postgres',
    database: 'postgres',
    port: 5432,
    dialectModule: pg,
    dialect: 'postgres',
    pool: {
      max: 100,
      min: 0,
      acquire: 10000,
      idle: 20000
    },
    logging: false
  }
Wellerson
  • 167
  • 1
  • 6