'Docker Redis Error: connect ECONNREFUSED 127.0.0.1:6379

I'm following a tutorial from youtube creating a simple dockerized CRUD application in Express, Node.js, Mongo & Redis. I stucked at video 3:06:57 on Authentication with sessions & Redis.

The initial issue I faced below when I send a POST

initial issue

ClientClosedError: The client is closed
    at Commander._RedisClient_sendCommand (/app/node_modules/@node-redis/client/dist/lib/client/index.js:408:31)
    at Commander.commandsExecutor (/app/node_modules/@node-redis/client/dist/lib/client/index.js:166:154)
    at Commander.BaseClass.<computed> [as set] (/app/node_modules/@node-redis/client/dist/lib/commander.js:8:29)
    at RedisStore.set (/app/node_modules/connect-redis/lib/connect-redis.js:65:21)
    at Session.save (/app/node_modules/express-session/session/session.js:72:25)
    at Session.save (/app/node_modules/express-session/index.js:406:15)
    at ServerResponse.end (/app/node_modules/express-session/index.js:335:21)
    at ServerResponse.send (/app/node_modules/express/lib/response.js:221:10)
    at ServerResponse.json (/app/node_modules/express/lib/response.js:267:15)
    at exports.login (/app/controllers/authController.js:45:28)
[nodemon] app crashed - waiting for file changes before starting...

I found out that the issue above was due to Redis version, the tutorial video was using Redis 3.0+ if not mistaken , but I'm having Redis 6.0+ .So I added the line redisClient.connect().catch(console.error); but second issue happen.

second issue

Error: connect ECONNREFUSED 127.0.0.1:6379
    at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1138:16) {
  errno: -111,
  code: 'ECONNREFUSED',
  syscall: 'connect',
  address: '127.0.0.1',
  port: 6379
}

the source files as below:

const express = require("express");
const mongoose = require("mongoose");
const session = require("express-session");
const redis = require("redis");
let RedisStore = require("connect-redis")(session);

const { MONGO_PASSWORD, MONGO_IP, MONGO_PORT, MONGO_USER, REDIS_URL, REDIS_PORT, SESSION_SECRET } = require("./config/config");


let redisClient = redis.createClient({
    host: REDIS_URL,
    port: REDIS_PORT,
})

// let redisClient = createClient({ 
//     host: REDIS_URL,
//     port: REDIS_PORT 
// });
 
//redisClient.connect().catch(console.error);

redisClient.on('error', err => {console.log('Error ' + err);});

version : "3"
services :
  node-app:
    build : .
    ports:
      - "3000:3000"
    environment:
      - PORT=3000
    depends_on:
      - mongo

  mongo:
    image: mongo
    environment:
      - MONGO_INITDB_ROOT_USERNAME=admin
      - MONGO_INITDB_ROOT_PASSWORD=password
    volumes:
      - mongo-db:/data/db

  redis:
    image: redis
    
volumes:
  mongo-db:

version : "3"
services :
  node-app:
    build:
      context: .
      args:
        NODE_ENV: development
    volumes:
      - ./:/app
      - /app/node_modules
    environment:
      - NODE_ENV=development
      - MONGO_USER=admin
      - MONGO_PASSWORD=password
      - SESSION_SECRET=secret
      
    command: npm run dev
  mongo:
    environment:
      - MONGO_INITDB_ROOT_USERNAME=admin
      - MONGO_INITDB_ROOT_PASSWORD=password

module.exports = {
    MONGO_IP: process.env.MONGO_IP || "mongo",
    MONGO_PORT: process.env.MONGO_PORT || 27017,
    MONGO_USER: process.env.MONGO_USER,
    MONGO_PASSWORD: process.env.MONGO_PASSWORD,
    REDIS_URL: process.env.REDIS_URL || "redis",
    REDIS_PORT: process.env.REDIS_PORT || 6379,
    SESSION_SECRET: process.env.SESSION_SECRET 
}

I did a check to the redis container logs and it's up and running redis-container logs. Also it should be in the same network with the node-app.

docker command : docker-compose -f docker-compose.yml -f docker-compose.dev.yml up -d

Thanks in advance for assisting.

Full source code from tutor https://github.com/Sanjeev-Thiyagarajan/node-docker



Solution 1:[1]

Redis client doesn't work in v4 of Redis. Replace the redisClient code with this:

let redisClient = redis.createClient({
    legacyMode: true,
    socket: {
        port: REDIS_PORT,
        host: REDIS_URL
    }
})

redisClient.connect().catch(console.error)

Solution 2:[2]

Connection refused means that the app that is calling the redis server is not working

i suspect that the issue lies on that ip address that the node-app container resolves, i tried the given repo from github.com/Sanjeev-Thiyagarajan/node-docker. I tunnel throgh the shell of the node-app container and tried to ping "redis" and the ip resolves to 172.18.0.3 which is based on the bridge network.

Please refer to the screenshot here

Could you upload the source code from your end? I suspect the you hard-coded the 127.0.0.1 somewhere in your code

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Facundo De Lucia
Solution 2