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"
2
Answers
I have this setup for redis and redis commander. Try it with docker compose -up
Iguess it’s because you have to use:
nestjs bull screenshoot