Search code examples
dockertcpdocker-composemicroservicesnestjs

Talking to Nest.js microservice over tcp in docker-compose


Code and more documentation can be found in this repo

Expected behavior:

Receive a response when running the app with and without docker.

What I got:

A response when running the app without docker but not inside docker.

What I think the problem could be:

The app seems to be working inside docker but the port just not passing through correctly.

What I already tried:

  • Making a hybrid app. Make a GET request that is then internally passed to the TCP micro-service (this worked but is not the behavior I want).
  • Run yarn start:dev inside the docker container instead of yarn start:prod. This did nothing, but then again the ports that where used where the same.
  • Exposing the port like so: (this did nothing)
- target: 3000
  published: 3000
  protocol: tcp
  mode: host

Solution

  • As this issue (this comment) explains

    If no host is specified, NestJS will bind to localhost

    This caused me to be unable to connect with the service while it was running in docker. So by setting the host to 0.0.0.0 I was able to connect over TCP.

    Full example:

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