Sling Academy
Home/Node.js/Fixing Docker + Node + Sequelize ECONNREFUSED Error

Fixing Docker + Node + Sequelize ECONNREFUSED Error

Last updated: December 29, 2023

Understanding the ECONNREFUSED Error

The ECONNREFUSED error typically indicates that a connection attempt has failed because the target machine actively refused it. This can happen when using Sequelize with Node.js in a Dockerized environment if Sequelize attempts to connect to the database before it’s ready or if there are network issues preventing a successful connection.

Checking Container Dependencies

One common fix is to ensure that your Node.js application waits for the database container to be fully up and running before it tries to establish a connection. Docker Compose can be used to manage container dependencies with the depends_on attribute.

Configuring Network Settings

Another solution is to check your Docker network settings. Ensure that your Node.js app is on the same network as your database service and that the service names are correctly used as hostnames in your Sequelize configuration.

Sample Docker-Compose Configuration

Below is an example of a docker-compose.yml file designed to ensure proper order of service startup and network configuration:

version: '3'
services:
  db:
    image: postgres
    networks:
      - back-tier
  app:
    build: .
    command: npm start
    depends_on:
      - db
    networks:
      - back-tier
networks:
  back-tier:
    driver: bridge

With this configuration, the app service will wait for the db service to be available before starting. Additionally, both services are part of the same network, ensuring they can communicate with each other.

Adjusting Sequelize Configuration

In your Sequelize configuration, you would use the service name as the host. Here’s a code snippet for the Sequelize config:

const sequelize = new Sequelize('database', 'username', 'password', {
  host: 'db',
  dialect: 'postgres',
  //... other config
});

Note that ‘db’ is the service name defined for the database in the docker-compose.yml file. Adjusting this configuration can often resolve the ECONNREFUSED error.

Next Article: Node & Sequelize: Fixing Model Association Column Creation Issue

Previous Article: How to Use Sequelize with TypeScript

Series: Sequelize.js Tutorials

Node.js

You May Also Like

  • NestJS: How to create cursor-based pagination (2 examples)
  • Cursor-Based Pagination in SequelizeJS: Practical Examples
  • MongooseJS: Cursor-Based Pagination Examples
  • Node.js: How to get location from IP address (3 approaches)
  • SequelizeJS: How to reset auto-increment ID after deleting records
  • SequelizeJS: Grouping Results by Multiple Columns
  • NestJS: Using Faker.js to populate database (for testing)
  • NodeJS: Search and download images by keyword from Unsplash API
  • NestJS: Generate N random users using Faker.js
  • Sequelize Upsert: How to insert or update a record in one query
  • NodeJS: Declaring types when using dotenv with TypeScript
  • Using ExpressJS and Multer with TypeScript
  • NodeJS: Link to static assets (JS, CSS) in Pug templates
  • NodeJS: How to use mixins in Pug templates
  • NodeJS: Displaying images and links in Pug templates
  • ExpressJS + Pug: How to use loops to render array data
  • ExpressJS: Using MORGAN to Log HTTP Requests
  • NodeJS: Using express-fileupload to simply upload files
  • ExpressJS: How to render JSON in Pug templates