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

38.2k views Asked by At

I'm trying to allow communication between my nodeJs docker image with my redis docker image (Mac OS X environment):

nodeJs Dockerfile:

FROM node:4.7.0-slim
EXPOSE 8100
COPY . /nodeExpressDB
CMD ["node", "nodeExpressDB/bin/www"]

redis Dockerfile:

FROM ubuntu:14.04.3
EXPOSE 6379
RUN apt-get update && apt-get install -y redis-server

nodeJs code which is trying to connect to redis is:

var redis = require('redis');
var client = redis.createClient();

docker build steps:

docker build -t redis-docker .
docker build -t node-docker .

docker run images steps flow:

docker run -p 6379:6379 redis-docker
docker run -p 8100:8100 node-docker

ERROR:

Error: Redis connection to 127.0.0.1:6379 failed - connect ECONNREFUSED 127.0.0.1:6379
    at Object.exports._errnoException (util.js:907:11)
    at exports._exceptionWithHostPort (util.js:930:20)
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1078:14)

What should I do inorder to connect to Redis from node-docker?

7

There are 7 answers

1
Niloct On

When connecting in node, use redis-docker in the function argument you pass the server IP.

2
Matt On

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
0
Erin Deji On

Solution

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

Then rebuild ur docker with => docker-compose up --build

1
Ananth On

If you can create a new redis docker instance, try mapping the container port to host:

docker run --name some-redis -p 6379:6379 -d redis

docker container start some-redis

Now, you can start the container and connect with host 127.0.0.1

const redis = require("redis");
const client = redis.createClient({
    host: '127.0.0.1',
    port: '6379'
});
0
Quince Ngomane On
  1. Download the redis server.
  2. run the redis server.
  3. and then run your project.

It should work just fine. Here's the download link:

Github - Redis Download Packages

I hope it works.

0
nevosial On

You can also change your /etc/hosts file to update the dockerip of the redis container.

Find the docker ip by using docker inspect

0
Damini Suthar On

if redis installed then run command

sudo apt-get install redis-server

Then you will get your site running.