0

I am trying to connect my Nestjs application to Redis as follows:

This the content of my docker-compose.yml

version: '3.7'

services:
  main:
    container_name: main
    build:
      context: .
      target: development
    volumes:
      - .:/usr/src/app
      - /usr/src/app/node_modules
    ports:
      - ${PORT}:${PORT}
      - 9229:9229
    command: yarn start:dev
    env_file:
      - .env

    networks:
      - webnet
  redis:
    image: 'redis:alpine'

networks:
  webnet:

In order to boostrap redis functionality in my nestjs application, I am using nestjs-redis and the relevant portion of app.module.ts looks like this:

import { RedisModule } from 'nestjs-redis';

@Module({
 imports: [
  RedisModule.register({})
 ]
})

However, when I try to run docker-compose up in my setup, I get the following error:

[ioredis] Unhandled error event: Error: connect ECONNREFUSED 127.0.0.1:6379

Thank you for helping!

1 Answer 1

3

As you are using docker-compose.yml, try to use the internal links capability.

version: '3.7'

services:
  main:
    container_name: main
    build:
      context: .
      target: development
    volumes:
      - .:/usr/src/app
      - /usr/src/app/node_modules
    ports:
      - ${PORT}:${PORT}
      - 9229:9229
    command: yarn start:dev
    links:
      - redis
    env_file:
      - .env
    networks:
      - webnet
  redis:
    image: 'redis:alpine'
    ports:
      - 6379:6379
    networks:
      - webnet

networks:
  webnet:

and in your snippet, pass the host in the following manner

import { RedisModule } from 'nestjs-redis';

@Module({
 imports: [
  RedisModule.register({host: "redis"})
 ]
})
Sign up to request clarification or add additional context in comments.

2 Comments

I've done the changes you outlined, the error message changes to [ioredis] Unhandled error event: Error: getaddrinfo ENOTFOUND redis, however, if I set the host to localhost in app.module.ts, it goes back to the original error message.
Just added the network in redis section as well and can you try now?

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.