简体   繁体   中英

Bull queue not connecting with docker redis service. Error: connect ECONNREFUSED 127.0.0.1:6379

I am trying to establish a redis connection in nestjs via docker. I am using ioredis to connect to the redis but when I start my nest application I am keep on getting ECONNREFUSED. It also looks like the bull queue is also not establishing the connection with redis.

Error: connect ECONNREFUSED 127.0.0.1:6379 at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1300:16)

I have went through many solutions provided but nothing seems to be working.

@Module({
  imports: [
    ConfigModule.forRoot({
      load: [redisConfig],
    }),
    BullModule.registerQueueAsync({
      name: 'jobs',
      imports: [ConfigModule.forFeature(bullQueueConfig)],
      useFactory: async (configService: ConfigService) => ({
        redis: {
          ...configService.get('bullQueue'),
        },
      }),
      inject: [ConfigService],
    }),
  ],
  controllers: [ConfigurationController],
  providers: [ConfigurationService, ConfigurationRepository],
  exports: [ConfigurationService],
})
export class ConfigurationModule {}

bull queue config

export default registerAs('bullQueue', () => {
  const redisURL =
    process.env.NODE_ENV === 'local'
      ? {
          host: process.env.BULL_QUEUE_REDIS_HOST,
          port: parseInt(process.env.BULL_QUEUE_REDIS_PORT ?? '6379'),
        }
      : JSON.parse(JSON.stringify(process.env.REDIS_URL));

  const env = {
    ...redisURL,
  };

  return env;

I get ECONNREFUSED error after the configuration module initialized.

In my.ts file

this.redisClient = new Redis({
        ...newRedisObj,
      });

newRedisObj also holds the correct values

{host: 'redis', port: 6379}

Redis config

export default registerAs('redis', () => {
  const redisURL =
    process.env.NODE_ENV === 'local'
      ? {
          host: process.env.REDIS_HOST,
          port: parseInt(process.env.REDIS_PORT ?? '6379'),
        }
      : JSON.parse(JSON.stringify(process.env.REDIS_URL));

  const env = {
    ...redisURL,
    
  };

  return env;

The config is returning the correct json with

{host: 'redis', port: 6379}

But it is still try to connect with 127.0.0.1:6379 and hence ECONNREFUSED.

The docker-compose has also the correct setup

redis:
    container_name: redis_container
    image: "bitnami/redis"
    environment:
      - ALLOW_EMPTY_PASSWORD=yes
    restart: unless-stopped
    ports:
      - "6379:6379"
    volumes:
      - "redis_data:/bitnami/redis/data"

I have this setup for redis and redis commander. Try it with docker compose -up

version: '3'
services:
  redis:
    image: 'redis:alpine'
    ports:
      - '6379:6379'
    volumes:
      - 'redis-data:/data'

  redis-commander:
    image: rediscommander/redis-commander:latest
    environment:
      - REDIS_HOSTS=local:redis:6379
    ports:
      - '8081:8081'
    depends_on:
      - redis

volumes:
  redis-data:
    external: false

Iguess it's because you have to use:

export default registerAs('redis', () => {
  const redisURL =
    process.env.NODE_ENV === 'local'
      ? {
          host: process.env.REDIS_HOST,
          port: parseInt(process.env.REDIS_PORT ?? '6379'),
        }
      : JSON.parse(JSON.stringify(process.env.REDIS_URL));

 // look here is surrounded by redis prop
  const env = {
    redis:...redisURL,
    
  };

  return env;

nestjs bull screenshoot

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM