Issue Docker with redis nodejs

I have issue is docker-compose up , and error : Error: connect ECONNREFUSED 127.0.0.1:6379 redis .

Please help me .

this config docker-compose.yml

version: '3.4'

services:
  app:
    image: chatapp/chatappbe:v1
    volumes:
      - ./:/app
    env_file: .env
    environment:
      - NODE_ENV=${NODE_ENV}
      - PORT=${PORT}
      - DATABASE=${DATABASE}
      - END_POINT=${END_POINT}
      - END_POINT_HOME=${END_POINT_HOME}
      - END_POINT_SERVER=${END_POINT_SERVER}
      - END_POINT_PATH=${END_POINT_PATH}
      - REDIS_URL=${REDIS_URL}

      - AWS_ACCESS=${AWS_ACCESS}
      - AWS_SECRET=${AWS_SECRET}
      - AWS_DOMAIN=${AWS_DOMAIN}
      - AWS_BUCKET_NAME=${AWS_BUCKET_NAME}
      - AWS_REGION=${AWS_REGION}

      - FB_ID=${FB_ID}
      - FB_KEY=${FB_KEY}

      - GG_ID=${GG_ID}
      - GG_KEY=${GG_KEY}

    restart: unless-stopped
    depends_on:
      - mongo
      - redis

  mongo:
    image: mongo:6.0.3
    container_name: chatapp_mongo
    volumes:
      - .docker/data/mongo:/data

  redis:
    image: redis:7.2-rc
    container_name: chatapp_redis
    volumes:
      - .docker/data/redis:/data

Hello,
127.0.0.1 from within the app-container is NOT the docker-host’s 127.0.0.1.
As your redis- and mongo-services do not provide ports to the docker-host (to the outside world) even the docker-host’s ip-address or the docker-networks-default-ip-address 172.17.0.1 (or on windows the hostname host.docker.internal) will not work.
But you should be able to use the service-name mongo and redis or the container’s-names chatapp_mongo and chatapp_redis to connect to from the app-container.

1 Like

Hello,
by default redis port 6379 is not exposed outside world if you are trying to access Redis that way. Redis is available within the container as its starting redis-server on startup. below is one example where internally its using redis and exposes app service.

https://docs.docker.com/compose/gettingstarted/

You are absolutely right, however, the error message was thrown by the app container and the containers were not using the host network or a common network namespace (network_mode: service:app) so as @matthiasradde answered, other containers should have referred to the redis container by the service name or container name in the same compose project.

1 Like