Error: Redis connection to 127.0.0.1:6379 failed – connect ECONNREFUSED 127.0.0.1:6379

Redis runs in a seperate container which has seperate virtual ethernet adapter and IP address to the container your node application is running in. You need to link the two containers or create a user defined network for them

docker network create redis
docker run -d --net "redis" --name redis redis
docker run -d -p 8100:8100 --net "redis" --name node redis-node

Then specify the host redis when connecting in node so the redis client attempts to connect to the redis container rather than the default of localhost

const redis = require('redis')
const client = redis.createClient(6379, 'redis')
client.on('connect', () => console.log('Connected to Redis') )

Docker Compose can help with the definition of multi container setups.

version: '2'
services:
  node:
    build: .
    ports:
    - "8100:8100"
    networks:
    - redis
  redis:
    image: redis
    networks:
    - redis
networks:
  redis:
    driver: bridge

Leave a Comment