skip to Main Content

I am simply connecting redis server from docker compose networks but its showing timeout error. I tried with localhost etc but its not working with that also. Here is my app.js

import express from "express"; 
import { createClient } from 'redis';

const app = express();
const port = process.env.PORT || 3000;
const apiPrefix = process.env.API_PRIFEX;

const client = await createClient(
{
    url: 'redis://redis:6379'
})
on('error', err => console.log('Redis Client Error', err))
.connect();

client.set('visits', 0);

app.get("/", (req, res) => {
client.get('visits', (err, visits) => {
res.send("Number of visits is " + visits)
client.set('visits', parseInt(visits) + 1);
})
res.status(200).json({ success: true, message: "Server1 running." });
});

app.listen(port, () => {
console.log(service is up and running on Port:${port});
});

But the issue is that it shows: connect ECONNREFUSED 127.0.0.1:6379

My docker-compose.yml looks like this I try with port and without port as well but nothing is working.

version: "3" 
    services:   
       redis:     
           image: "redis"     
           ports: 
               - "6379:6379"   
       node-app:     
           build: .     
               ports: 
                   - "4001:3000"` 

I have tried with different host name local host etc but in answers I have find I need to connect only with image name but still its not working.

2

Answers



  1. The issue that you are encountering is that the address 127.0.0.1 in a docker container refers to itself, not to the host OS.

    There are three ways you can solve that, both involve adding the network attribute:

    • You can run your nodejs container with the property network_mode: host. For more information refer to that answer from: Codestation
    • You can also create a link between your containers and you will be able to access your redis container from your container with the hostname redis or redisdb
    version: "3" 
        services:   
           redis:     
               image: "redis"     
               ports: 
                   - "6379:6379"   
           node-app:     
               build: .     
               ports: 
                   - "4001:3000"
               links:
                   - "redis:redisdb"
    
    • The last way would be to create your own network that can easily connect multiple containers with each other, and they will be able to access each other with their names as hostnames:
    version: "3" 
        services:   
           redis:     
               image: "redis"     
               ports: 
                   - "6379:6379"
               networks:
                   - mynetwork
           node-app:     
               build: .     
               ports: 
                   - "4001:3000"
               networks:
                   - mynetwork
        networks:
           mynetwork:
               name: mynetwork,
               driver: bridge
    
    

    I hope that this was helpful for details check the documentation

    Login or Signup to reply.
    • Create this files for redis in docker.

    docker-compose.yml

    version: '3.9'
    
    services:
      redis:
        container_name: redis-container
        image: redis:6.2.6
        restart: always
        ports:
          - 6379:6379
        volumes:
          - ./redis.conf:/usr/local/etc/redis/redis.conf
          - ./redis-backup:/data
        command: redis-server /usr/local/etc/redis/redis.conf
    
      node-app:
        container_name: node-app
        build:
          context: ./node-app
          dockerfile: Dockerfile
        command: bash -c "npm i && nodemon index.js"
        restart: always
        ports:
          - 3000:3000
        volumes:
          - ./node-app:/home/node-app
        depends_on:
          - redis
    
    • Create Dockerfile in node-app folder
      ./node-app/Dockerfile
    FROM node:18.16.0-bullseye as builder
    
    WORKDIR /home/node-app
    
    COPY . .
    
    RUN npm install 
        npm install -g @vercel/ncc
    
    RUN ncc build build/index.js -o dist
    
    FROM node:18.16.0-bullseye as release
    
    WORKDIR /home/node-app
    COPY --from=builder /home/node-app/dist .
    
    EXPOSE 3000
    
    CMD [ "node", "index.js" ]
    
    • Create redis-backup file folder.
    • Create redis.conf file.

    redis.conf

    protected-mode no
    port 6379
    requirepass mypassword
    maxmemory 1gb
    

    You can edit your files for this codes and files.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search