skip to Main Content

I’m struggling to connect a redis deployment to my nodejs app. Of course locally without the use of docker, it works well, so I’m at odds as to whether this is an issue to do with my code, or the way I’ve set up my docker compose file

Dockerfile:

FROM node:8
WORKDIR /app
COPY package.json /app
COPY . /app
RUN npm install
CMD ["npm", "start"]
EXPOSE 3000

docker-compose.yml

version: "3"
services:
  web:
    container_name: web-container
    restart: always
    depends_on:
      - redis
    build: . 
    ports: 
    - "3000:3000"
    links: 
      - redis
  redis:
    container_name: redis-container
    image: "redis:latest"
    ports:
      - "6379:6379"
    volumes:
      - ./data:/data

Redis Connection File (RedisService.js)

const redis         = require("redis");
const client        = redis.createClient();
const DbUtils       = require("../../db_utils");
const {promisify}   = require("util");
const getAsync      = promisify(client.get).bind(client);
const existsAsync   = promisify(client.exists).bind(client);

class RedisCache {
    constructor () {
        var connected;
        // * Initiliase the connection to redis server
        client.on("connect", () => {console.log("📒 Redis cache is ready"); connected = true;})
        client.on("error", (e) => {console.log("Redis cache error:n" + e); connected = false;});
    }

    async setData (id, data) {
        // * Stringify data if it's an object
        data = data instanceof Object ? JSON.stringify(data) : data;
        client.set(id, data);
        return true;
    }

    async getData (key) {
        return getAsync(key).then(data => {
            data = JSON.parse(data) instanceof Object ? JSON.parse(data) : data;
            return data; 
        })
    }

    async exists (key) {
        return existsAsync(key).then(bool => {
            return bool;
        })
    }

    // Returns status of redis cache
    async getStatus () {
        return this.connected;
    }
}

module.exports = new RedisCache();

ERROR
Error: Redis connection to 127.0.0.11:6379 failed – connect ECONNREFUSED 127.0.0.11:6379

2

Answers


  1. When you run your containers via docker-compose they are all connected to a common network. Service name is a DNS name of given container so to access redis container from web you should create the client like :

    const client = redis.createClient({
            port      : 6379,
            host      : 'redis'
    });
    

    You have not configured the host so it uses the default one – 127.0.0.1. But from the point of view of your web container the redis is not running on the localhost. Instead it runs in it’s own container which DNS name is redis.

    Login or Signup to reply.
  2. The beginning (docker part) of this tutorial worked for me :
    https://medium.com/geekculture/using-redis-with-docker-and-nodejs-express-71dccd495fd3

    docker run -d --name <CONTAINER_NAME> -p 127.0.0.1:6379:6379 redis
    

    then in the node server (like in official redis website example) :

    const redis = require('redis');
    
    async function start() {
        const client = redis.createClient(6379,'127.0.0.1');
        await client.connect();
    
        await client.set('mykey', 'Hello from node redis');
        const myKeyValue = await client.get('mykey');
        console.log(myKeyValue); 
    }
    
    start();
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search