nest: Cannot connect dockerised microservices after v.6.0.5

Regression

Potential Commit/PR that introduced the regression**

6.1.0?

Describe the regression

I was struggling a while after upgrading NestJS to latest 6.4.0 with microservices that run inside docker. After downgrading @nestjs/microservices to version 6.0.5 it all started working fine. Tried few releases in between but didn’t manage to get it working. I did not change any code. Just following the example. With latest NestJS version I’m getting error connect ECONNREFUSED

Input Code

Code that works on v.6.0.5, nothing special, following microservice example from https://docs.nestjs.com/microservices/basics

server main.ts ->

    const msPort: number = 3001;
    const appAsMicroservice: INestMicroservice = await NestFactory.createMicroservice(ApplicationModule, {
        transport: Transport.TCP,
        options: { port: msPort }
    });
    await appAsMicroservice.listen(() => console.log('microservice is ready at: ' + msPort));

tried connecting the client with both “module way” … ->

@Module({
    imports: [
        ClientsModule.register([
            {
                name: 'my_microservice',
                transport: Transport.TCP,
                options: { port: 3001, host: 'module_docker_name' }
            }
        ])
    ]

with @Inject ->

constructor(@Inject('module_docker_name') private readonly microserviceClient: ClientProxy) {}

… and alternatively using ClientProxyFactory.create() ->

        this.microserviceClient = ClientProxyFactory.create({
            transport: Transport.TCP,
            options: {
                host: 'module_docker_name',
                port: 3001
            }
        });

Both methods work on NestJS v.6.0.5 but not after that

Expected behavior/code

Microservices should work also between docker containers

Environment


Nest version: 6.0.5 -> 6.4.0

For Tooling issues:
- Node version: 8.12.0  
- Platform:  Mac Mojave 10.14.4

Others:

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 1
  • Comments: 17 (4 by maintainers)

Most upvoted comments

I was struggling to get my microservices working fine in Google Kubernetes Engine last week until, for some random enlightening I got to identify what to change in order to access a Dockerized microserviced app.

If no host is specified, NestJS will bind to localhost as defined in constants.ts.:

https://github.com/nestjs/nest/blob/a0a51302ed3d13476e5e19c65c3a3cc07bae6f42/packages/microservices/constants.ts#L2

Changing the host in the createMicroservice function to 0.0.0.0 seems to work just fine even with different Nest versions (6.5.3 connecting to 6.5.2).

const app = await NestFactory.createMicroservice(AppModule, {
    transport: Transport.TCP,
    options: {
      host: '0.0.0.0',
      port: 3000
    }
  });

@GabrielGil you saved my life man, I’d racked up about 20 hours trying to figure out why my services couldn’t communicate. I least im pretty familiar with every single page of the kubernetes docs now…