skip to Main Content

I am trying to develop an express api.It works on local machine as expected. I am using docker but on production with docker and heroku redis is not working

Dockerfile

FROM node:latest

WORKDIR /usr/src/app

COPY package*.json ./

RUN npm install 

COPY . .

EXPOSE 5000

CMD ["npm","start"]

docker.compose.yml file

version: '3'
services:

  mongo:
    container_name: mongo
    image: mongo
  
    ports:
      - '27017:27017'
  redis:
    container_name: redis
    image: redis
    

  app:
    container_name: password-manager-docker
    image: app
    restart: always
    build: .
    
    ports:
      - '80:5000'
    links: 
      - mongo
      - redis
    environment: 
      MONGODB_URI: ${MONGODB_URI}
      REDIS_URL: ${REDIS_URL}   
      clientID: ${clientID}
      clientSecret : ${clientSecret}
      PORT: ${PORT}
      REDIS_HOST: ${REDIS_HOST}
      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}
      SMTP_HOST: ${SMTP_HOST}
      SMTP_PORT: ${SMTP_PORT}
      SMTP_USER: ${SMTP_USER}
      SMTP_PASS: ${SMTP_PASS}

redis file

const asyncRedis = require('async-redis');
//process.env.REDIS_HOST's value is redis
const redisClient = asyncRedis.createClient({port:6379,host:process.env.REDIS_HOST || "127.0.0.1"});
redisClient.on("connect",() => {
  console.log(`Redis: ${host}:${port}`);
})
redisClient.on('error', function(err) {
  // eslint-disable-next-line no-console
  console.log(`[Redis] Error ${err}`);
});

The error on heroku is "[Redis] Error Error: Redis connection to 127.0.0.1:6379 failed – connect ECONNREFUSED 127.0.0.1:6379". It worked without docker on heroku but not It is not working. Thanks for your help

3

Answers


  1. Chosen as BEST ANSWER

    I finally figured it out.I changed redis addon in heroku from heroku redis to redis to go and changed this line

    const redisClient = asyncRedis.createClient({port:6379,host:process.env.REDIS_HOST || "127.0.0.1"});
    

    to

    const redisClient = asyncRedis.createClient(process.env.REDISTOGO_URL);
    

    1. Install redis

      sudo apt-get install redis-server

    2. Run command to check if everything is fine

      sudo service redis-server status

    And if you get the message redis-server is running then it’ll resove

    Login or Signup to reply.
  2. The environment variable REDIS_HOST isn’t set – you have the answer already in your question, error is: ECONNREFUSED 127.0.0.1:6379
    The hostaddress is comming from your if statement: {port:6379,host:process.env.REDIS_HOST || "127.0.0.1"}
    If process.env.REDIS_HOST is not set, then use 127.0.0.1 as hostaddress. You have to parse a config file or set it via -e KEY=VAL when you run docker-compose or just edit the line REDIS_HOST: ${REDIS_HOST} and replace ${REDIS_HOST} with the service name (redis) from the docker-compose.yml file or…, there are a lot of possibilities. If you want to know the real ip of the redis container, just check with docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' <CONTAINER_ID/NAME>

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search