Lucas Cafieiro
Lucas Cafieiro

Reputation: 55

Docker connection refused between containers

I'm using Postgres, Redis and Node.js (adding dependencies with yarn), and trying to integrate it all with a docker-compose.yml file.

I have the following docker-compose.yml:

version: '3'

services:
  postgres:
    image: postgres:latest
    restart: unless-stopped
    environment:
      - POSTGRES_DB=mybase
      - POSTGRES_USER=myuser
      - POSTGRES_PASSWORD=mypass

  redis:
    image: redis:latest
    restart: unless-stopped

  migrate:
    build: .
    entrypoint: node_modules/.bin/sequelize db:migrate --config src/config/database.js --migrations-path src/database/migrations/
    volumes:
      - ./:/app
      - /app/node_modules
    depends_on:
      - postgres

  wavetech-be:
    build:
      dockerfile: Dockerfile
      context: .
    restart: on-failure
    volumes:
      - /app/node_modules
      - ./:/app
    environment: 
      - REDIS_HOST=redis
      - REDIS_PORT=6379
      - DB_HOST=postgres
      - DB_USER=myuser
      - DB_PASS=mypass
      - DB_PORT=5432
      - DB_NAME=mybase
    depends_on:
      - redis 
      - migrate

And the following Dockerfile:

FROM node:alpine

WORKDIR "/app"

COPY ./package.json ./
RUN apk add yarn
RUN yarn

COPY . .

CMD [ "yarn", "dev" ]

However, when I docker-compose up, I keep getting connection problems with both databases:

migrate_1      | 
migrate_1      | ERROR: connect ECONNREFUSED 127.0.0.1:5432
migrate_1      | 
...
wavetech-be_1  | (node:85) UnhandledPromiseRejectionWarning: Error: connect ECONNREFUSED 127.0.0.1:6379

Upvotes: 0

Views: 460

Answers (1)

Lucas Cafieiro
Lucas Cafieiro

Reputation: 55

The answer has two parts:

First, as pointed out by @jonrsharpe, the description of the migrate service was lacking the environment variables. So, as it is with the volumes, each service needs its own environment variables configured.

  migrate:
    build: .
    entrypoint: node_modules/.bin/sequelize db:migrate --config src/config/database.js --migrations-path src/database/migrations/
    volumes:
      - ./:/app
      - /app/node_modules
    environment:
      - DB_HOST=postgres
      - DB_USER=myuser
      - DB_PASS=mypass
      - DB_PORT=5432
      - DB_NAME=mybase
      - APP_PORT=3000
    depends_on:
      - postgres

Second, I am using Bull to manage my Redis server. I was importing a config and passing it directly to Redis, so:

import redisConfig from '../../config/redis';
...
  init() {
    this.queues = Object.values(jobs).map(job => ({
      bull: new BullQueue(job.key, redisConfig),
      name: job.key,
      handle: job.handle,
    }));
  }

And it turns out that Bull was trying to just use the default Redis configuration. When I passed the environment variables directly into the Bull config, it worked properly:

  init() {
    this.queues = Object.values(jobs).map(job => ({
      bull: new BullQueue(job.key, {
        redis: {
          host: process.env.REDIS_HOST,
          port: process.env.REDIS_PORT,
        },
      }),
      name: job.key,
      handle: job.handle,
    }));
  }

Upvotes: 1

Related Questions