4

I have an Express app and React app, and in the backend part I'm using Redis. I setup one Dockerfile for the frontend, and one for the backend. Additionally, I setup the docker-compose.yml file, which looks like this:

# Specify docker-compose version.
version: '3'

# Define the services/containers to be run.
services:
  react:
    build: admin
    ports:
      - '3000:3000'

  express:
    build: .
    container_name: api
    ports:
      - '3001:3001'
    depends_on:
      - redis
    links:
      - mongo
      - redis
    environment:
      - REDIS_URL=redis://cache
      - MONGO_URL=mongodb://db/tests

  mongo:
    image: mongo:4
    container_name: db
    ports:
      - '27017:27017'

  redis:
    image: redis:4
    container_name: cache
    ports:
      - '6379:6379'

And inside my backend, I call redisClient as follows:

const bluebird = require('bluebird');
const config   = require('config');
const logger   = require('./logger');
const redis    = require('redis');
bluebird.promisifyAll(redis);

const RedisService = function() {
    const redisConnectionString = process.env.REDIS_URL;
    this.client = redis.createClient({ url: redisConnectionString });
    this.client.on('error', (err) => {
        logger.error(err);
    });
};

Where config reads the .json file inside my config folder. However, when I run docker-compose up, it throws the following error:

express_1  | [2019-06-10T20:14:38.169Z] error: "uncaughtException: Redis connection to 127.0.0.1:6379 failed - connect ECONNREFUSED 127.0.0.1:6379
Error: Redis connection to 127.0.0.1:6379 failed - connect ECONNREFUSED 127.0.0.1:6379
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1106:14) 

Any ideas how to properly connect Redis with docker-compose in my setting where I read the connection string from the config .json file?

2
  • how does redisConnectionString look like Commented Jun 11, 2019 at 6:12
  • @EfratLevitan I just updated my question with the current version of the docker-compose file and the Redis connection string. Commented Jun 12, 2019 at 14:36

1 Answer 1

6

From the logs it seems that it tries to connect to REDIS on localhost (127.0.0.1). The express docker container can reach REDIS by service name which is redis.

Try to replace localhost with redis in redisConnectionString. Something like:

redis://[[user][:password@]]redis:6379

Hopefully that will solve your problem.

Sign up to request clarification or add additional context in comments.

3 Comments

I tried but doesn't seem to work. See my updated question. It still shows the same error. It still somehow tries to connect to localhost.
It should work with container_name as well. Asking the obvious, but did you log redisConnectionString to make sure its value is what you expect i.e. redis://cache?
Thanks. Apparently, I forgot that I was using the kue package also, and it was automatically trying to connect to Redis using localhost, hence I needed to update the kue settings too.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.