redisnestjs

Can't Connect to Redis in Docker with NestJS


I am trying to connect to a Redis instance running in a Docker container from my NestJS application. However, I'm encountering connection issues when using the URL for the Redis connection. When I configure the connection using host and port, it works successfully. Additionally, the same URL connection works fine when I run the application on my local machine, outside of Docker.

app.module.ts

import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';

import * as redisStore from 'cache-manager-ioredis';
import { CacheModule } from '@nestjs/cache-manager';
import { RedisClientOptions } from 'redis';

@Module({
  imports: [
    CacheModule.registerAsync<RedisClientOptions>({
      isGlobal: true,
      useFactory: async () => {
        return {
          store: redisStore,
          url: 'redis://:@redis:6379/0',
          // host: 'redis',
          // port: 6379,
        };
      },
    }),
  ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}
docker-compose.yml

services:
  redis:
    image: redis:alpine3.20
    restart: always
    ports:
      - 6378:6379
    volumes:
      - ./volumes/redis/data:/data

  app:
    build:
      context: .
      dockerfile: Dockerfile.dev
      target: release
    ports:
      - 2080:3000
    entrypoint: ['yarn', 'start:prod']

dependencies

"@nestjs/cache-manager": "^2.2.2",
"@nestjs/common": "^10.0.0",
"@nestjs/config": "^3.2.3",
"@nestjs/core": "^10.0.0",
"@nestjs/jwt": "^10.2.0",
"@nestjs/mapped-types": "*",
"@nestjs/platform-express": "^10.0.0",
"@nestjs/typeorm": "^10.0.2",
"cache-manager": "^5.7.6",
"cache-manager-ioredis": "^2.1.0",
"class-transformer": "^0.5.1",
"class-validator": "^0.14.1",
"pg": "^8.12.0",
"redis": "^4.7.0",
"reflect-metadata": "^0.2.0",
"rxjs": "^7.8.1",
"typeorm": "^0.3.20"

I want connect with redis by url


Solution

  • Docker compose yml file

    version: '3.3'
    name: 'docker'
    
    services:
      redis:
        image: redis:6.0.7
        command: redis-server --requirepass Admin12
        ports:
          - 6380:6379
        volumes:
          - ./redis.conf:/usr/local/etc/redis/redis.conf
    

    make sure you install the "cache-manager-redis-store": "^2.0.0"

    import { CacheModule } from '@nestjs/cache-manager';
    import { Module } from '@nestjs/common';
    import * as redisStore from 'cache-manager-redis-store';
    import { CacheStoreService } from './cache.service';
    
    @Module({
      imports: [
        CacheModule.registerAsync({
          useFactory: () => ({
            store: redisStore,
            host: 'localhost',
            port: 6380,
            password: 'yourpassword',
          }),
        }),
      ],
      providers: [CacheStoreService],
      exports: [CacheStoreService],
    })
    export class CacheStoreModule {}
    

    If you want to connect using URL

    import { CacheModule } from '@nestjs/cache-manager';
    import { Module } from '@nestjs/common';
    import * as redisStore from 'cache-manager-redis-store';
    import { CacheStoreService } from './cache.service';
    
    @Module({
      imports: [
        CacheModule.registerAsync({
          useFactory: () => ({
            store: redisStore,
            // host: 'localhost',
            // port: 6380,
            // password: 'yourpassword',
            url: 'redis://default:yourpassword@localhost:6380',
          }),
        }),
      ],
      providers: [CacheStoreService],
      exports: [CacheStoreService],
    })
    export class CacheStoreModule {}