Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

1.3K
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda