2
votes

Can anyone solve this problem? I'm a node.js and redis beginner.

The function createClient() can take 2 arguments: port and host. But I'm still stuck with this problem.

var client = redis.createClient(); 

client.on('connect', function(){
    console.log('Redis client connected');
});

Error: Redis connection to 127.0.0.1:6379 failed - connect ECONNREFUSED 127.0.0.1:6379 C:\Users\my-pc\Desktop\FINAL>node index events.js:187 throw er; // Unhandled 'error' event ^

Error: Redis connection to 127.0.0.1:6379 failed - connect ECONNREFUSED 127.0.0.1:6379
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1128:14) Emitted 'error' event on RedisClient instance at: at RedisClient.on_error (C:\Users\my-pc\Desktop\FINAL\node_modules\redis\index.js:406:14) at Socket. (C:\Users\my-pc\Desktop\FINAL\node_modules\redis\index.js:279:14) at Socket.emit (events.js:210:5) at emitErrorNT (internal/streams/destroy.js:92:8) at emitErrorAndCloseNT (internal/streams/destroy.js:60:3) at processTicksAndRejections (internal/process/task_queues.js:80:21) { errno: 'ECONNREFUSED',
code: 'ECONNREFUSED', syscall: 'connect', address: '127.0.0.1',
port: 6379 }

2
What is the actual problem? - Dragonthoughts
Redis only config on port 6379. And even in my index.js file, the app listen on port 6379 still causes this problem. - Nguyen DucAnh
is your redis service running? - Babak Abadkheir

2 Answers

4
votes

This is a very common but simple issue whenever i had also started working with docker multiple local containers.

See, you have created a node app and also a redis server, but both are separated, you didn't build any communication between them, that's why you are facing this type of problems.

Now, first you have to build a connection between the node app and redis-server so that they can communicate between then.

For doing this, just simply follow the instructions, hopefully this will solve your issues.

Step-1: For building connection between multiple local containers, you have two options to do this,

* Use docker CLI's networking features.
* Or, simply use docker-compose to solve the issue, i prefer docker-compose and i will show how to do this using docker-compose.

Docker-compose: Compose is a tool for defining and running multi-container docker applications. Overview of docker-compose

Step-2: Make a docker-compose .yml file in your project directory, named by "docker-compose.yml"

Step-3: Write the following code in your "docker-compose.yml" file:

version: "2.0"
services: 
  redis-server: 
    image: "redis"
  node-app: 
    build: .
    ports: 
      - "8080:8080"

Step-4: Inside your redis.createClient() funstion, simply pass your host and port parameters in index.js file

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

Step-5: Build and run your docker container using docker-compose from docker-compose CLI

docker-compose up --build

Step-6: And finally, open your browser and hit the following URL:

localhost:8080
3
votes

Based on the error code being 'ECONNREFUSED' are you positive that you're actually running a redis-server on localhost and port 6379 before you try to connect the client?