Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

1.3K
Views
Docker-Compose: Unable to connect NodeJS with Mongo & Redis [Connection Refused]

I am working with 3 Services:

  • api-knotain [main api service]
  • api-mongo [mongo db for api service]
  • api-redis [redis for api service]

The Dockerfile for api-knotain looks as follows

FROM node:latest
# Create app directory
WORKDIR /usr/src/app
# Install app dependencies
# A wildcard is used to ensure both package.json AND package-lock.json are copied
# where available (npm@5+)
COPY package*.json ./
RUN npm install
# If you are building your code for production
# RUN npm ci --only=production
# Bundle app source
COPY . .
CMD [ "npm", "start" ]

my docker-compose file as such:

version: '3.3'
services:
  api-knotain:
    container_name: api-knotain
    restart: always
    build: ../notify.apiV2/src
    ports:
      - "7777:7777"
    links:
      - api-mongo
      - api-redis
    environment:
      - REDIS_URI=api-redis
      - REDIS_PORT=32770
      - MONGO_URI=api-mongo
      - MONGO_PORT=27017
      - RESEED=true
      - NODE_TLS_REJECT_UNAUTHORIZED=0
  api-mongo: 
    container_name: api-mongo
    image: mongo
    volumes:
      - ./data:/data/db
    ports:
      - "27017:27017"
  api-redis:
    container_name: api-redis
    image: "redis:alpine"
    ports:
      - "32770:32770"

runnin

  • docker-compose build
  • docker-compose up

output:

api-knotain    | connecting mongo ...: mongodb://api-mongo:27017/notify
api-knotain    | Redis error: Error: Redis connection to api-redis:32770 failed - connect ECONNREFUSED 172.21.0.2:32770
api-knotain    | mongo error:MongoNetworkError: failed to connect to server [api-mongo:27017] on first connect [MongoNetworkError: connect ECONNREFUSED 172.21.0.3:27017]
api-knotain    | Example app listening on port 7777!

neither mongo nor redis can be connected. I tried the following things:

  • use localhost instead of container name
  • use different ports
  • use expose vs port

always with the same result

note:

  • i can connect without issue to both mongo & redis through local cli 'localhost:port'

what am I missing?

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Probably redis and mongo containers start later than you application and therefore your app will not see those. To counter this you must wait for those services to be ready.

Also links is a legacy feature of Docker. You should use depends_on to control startup order and user-defined networks if you want to isolate your database and redis containers from external network.

over 4 years ago · Santiago Trujillo Report

0

version: '3.3'
services:
  api-knotain:
    depends_on:
      - api-mongo
      - api-redis
    container_name: api-knotain
    restart: always
    build: ../notify.apiV2/src
    ports:
      - "7777:7777"
    links:
      - api-mongo
      - api-redis
    environment:
      - REDIS_URI=api-redis
      - REDIS_PORT=32770
      - MONGO_URI=api-mongo
      - MONGO_PORT=27017
      - RESEED=true
      - NODE_TLS_REJECT_UNAUTHORIZED=0
  api-mongo: 
    container_name: api-mongo
    image: mongo
    volumes:
      - ./data:/data/db
    ports:
      - "27017:27017"
  api-redis:
    container_name: api-redis
    image: "redis:alpine"
    ports:
      - "32770:32770"
over 4 years ago · Santiago Trujillo Report

0

It looks like depend_on did not properly work in 3.3 version of docker compose. after updating the version to 3.7 all works perfectly without any changes to the compose file.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!