question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cannot connect dockerised microservices after v.6.0.5

See original GitHub issue

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:

Issue Analytics

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

github_iconTop GitHub Comments

4reactions
GabrielGilcommented, Jul 22, 2019

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
    }
  });
2reactions
mattwills8commented, Sep 20, 2019

@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…

Read more comments on GitHub >

github_iconTop Results From Across the Web

No results found

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found