0
votes

I have been trying to dockerize an api . But redis crashes. Nodejs and mongodb work.

Docker-compose.yaml file

version: '3'
services:

  mongo:
    container_name: mongo
    image: mongo
    networks:
      - webnet
    ports:
      - '27017:27017'
  redis: 
    image: redis
    container_name: redis
    command: ["redis-server","--bind","redis","--port","6379"]
    ports:
      - '6379:6379'
    hostname: redis

  app:
    container_name: password-manager-docker
    restart: always
    build: .
    networks:
      - webnet
    ports:
      - '80:5000'
    links: 
      - mongo
      - redis
    environment: 
      MONGODB_URI: ${MONGODB_URI}
      clientID: ${clientID}
      clientSecret : ${clientSecret}
      PORT: ${PORT}
      NODE_ENV : ${NODE_ENV}
      JWT_SECRET_KEY: ${JWT_SECRET_KEY}
      JWT_EXPIRE: ${JWT_EXPIRE}
      REFRESH_TOKEN: ${REFRESH_TOKEN}
      JWT_REFRESH_SECRET_KEY: ${JWT_REFRESH_SECRET_KEY}
      JWT_REFRESH_EXPIRE: ${JWT_REFRESH_EXPIRE}
      JWT_COOKIE: ${JWT_COOKIE}
       
networks:
  webnet:

Docker file

FROM node:latest

WORKDIR /usr/src/app

COPY package*.json ./

RUN npm install 

COPY . .

EXPOSE 5000

CMD ["npm","start"]

The error is Redis connection to 127.0.0.1:6379 failed - connect ECONNREFUSED 127.0.0.1:6379. How can I fix this ?

1
From the application try to connect to redis:6379 instead of 127.0.0.1 - Krzysztof Safjanowski
I've tried. But the same error.Redis connection to 6379 failed - Oğulcan Karayel
Maybe other service is occupying that port? What is the result of ‘docker ps’? - Krzysztof Safjanowski
There are 3 container and ports 0.0.0.0:80->5000/tcp ,0.0.0.0:6379->6379/tcp (redis container) ,0.0.0.0:27017->27017/tcp - Oğulcan Karayel
You are overriding the default CMD for redis with command. It should work out-of-the-box if you don't mess with the command that is used for starting the container. - Marko E

1 Answers

0
votes

by default all containers in a compose file will join a default network where they can communicate with each other, but if you specify a network then you have to specify it for all. So you can either remove the network declaration from the app and mongo services or specify it on the redis service.