title: NestJS: Microservices with Docker
body: <p>I'm working with microservices and I have a problem. I have two NestJS applications with microservices. First one is <code>hybrid appliacation</code> and the second one is just a simple microservices with <code>app.listen()</code> function. Everything is set up in Docker-compose. I just want to send a simple <code>@EventPattern</code> and it fails. Second application don't receive any event. I was following this topic: <a href="https://github.com/nestjs/nest/issues/2532" rel="nofollow noreferrer">https://github.com/nestjs/nest/issues/2532</a> but it didn't help at all. Here is some code I wrote:</p>
  
  <p><code>docker-compose-dev.yml</code></p>
  
  <pre class="lang-yaml prettyprint-override"><code># First Server
    # --------------------------------------------------
    first-server:
      container_name: first-server
      image: project/firstserver
      build:
        context: ../first-server/
        args:
          UUID: ${UUID}
          UGID: ${GUID}
      environment:
        NODE_ENV: ${NODE_ENV:-development}
      depends_on:
        - db-postgres
        - db-mongo
      user: "${UUID}:${GUID}"
      tty: true
      expose:
        - "3000"
        - "3010"
      ports:
        - "3000:3000"
        - "3010:3010"
      volumes:
        - ../first-server:/app
      networks:
        - servers-network
  
    # Second Server
    # --------------------------------------------------
    second-server:
      container_name: second-server
      image: project/secondserver
      build:
        context: ../second-server/
        args:
          UUID: ${UUID}
          UGID: ${GUID}
      environment:
        NODE_ENV: ${NODE_ENV:-development}
      depends_on:
        - db-postgres
        - db-mongo
      user: "${UUID}:${GUID}"
      tty: true
      expose:
        - "3100"
        - "3020"
      ports:
        - "3100:3100"
        - "3020:3020"
      volumes:
        - ../second-server:/app
      networks:
        - servers-network
  
  # Docker networks
  # --------------------------------------------------
  networks:
    servers-network:
      driver: bridge
      name: servers-network
  </code></pre>
  
  <p><strong>first-server:</strong></p>
  
  <p><code>main.ts</code></p>
  
  <pre class="lang-js prettyprint-override"><code>async function bootstrap() {
      const app = await NestFactory.create&lt;NestExpressApplication&gt;(AppModule);
      const microservice = app.connectMicroservice({
          transport: Transport.TCP,
          options: {
              host: '0.0.0.0',
              port: 3010
          }
      });
  
      await app.startAllMicroservicesAsync();
      await app.listen(port);
  }
  </code></pre>
  
  <p><code>app.module.ts</code></p>
  
  <pre class="lang-js prettyprint-override"><code>@Module({
      imports: [
          TypeOrmModule.forRootAsync({
              useClass: TypeOrmPostgresConfigProvider,
              name: TypeOrmPostgresConfigProvider.connectionName,
              imports: [
                  ConfigModule
              ]
          }),
          TypeOrmModule.forRootAsync({
              useClass: TypeOrmMongoConfigProvider,
              name: TypeOrmMongoConfigProvider.connectionName,
              imports: [
                  ConfigModule
              ]
          }),
          ServeStaticModule.forRoot({
              rootPath: join(__dirname, '../..', 'public/app')
          }),
          ClientsModule.register([
              {
                  name: 'SERVICE_A',
                  transport: Transport.TCP,
                  options: {
                      host: '0.0.0.0',
                      port: 3010
                  }
              }
          ])
      ],
      controllers: [
          AppController
      ],
      providers: []
  })
  </code></pre>
  
  <p><code>app.controller.ts</code></p>
  
  <pre class="lang-js prettyprint-override"><code>export class AppController {
  
      constructor(
          @Inject('SERVICE_A') private readonly client: ClientProxy
      ) {
      }
  
      async onApplicationBootstrap() {
          await this.client.connect()
              .then(result =&gt; {
                  console.log('result');
              })
              .catch(error =&gt; {
                  console.log(error);
              });
      }
  
      @Get('hello')
      getHello() {
          this.client.emit&lt;any&gt;('message_printed', new Message('Hi there!'));
          return 'Hello World printed';
      }
  
      @EventPattern('message_printed')
      async handleMessagePrinted(data: Record&lt;string, unknown&gt;) {
          console.log('first-server event!');
      }
  
  }
  </code></pre>
  
  <p><strong>second-server:</strong></p>
  
  <p><code>main.ts</code></p>
  
  <pre class="lang-js prettyprint-override"><code>async function bootstrap() {
      const app = await NestFactory.createMicroservice&lt;MicroserviceOptions&gt;(
          AppModule,
          {
              transport: Transport.TCP,
              options: {
                  host: '0.0.0.0',
                  port: 3020
              }
          }
      );
  
      app.listen(() =&gt; console.log('Microservice is listening'));
  }
  </code></pre>
  
  <p><code>app.controller.ts</code></p>
  
  <pre class="lang-js prettyprint-override"><code>export class AppController {
  
      constructor() {
  
      }
  
      @EventPattern('message_printed')
      async handleMessagePrinted(data: Record&lt;string, unknown&gt;) {
          console.log('second-server event!');
      }
  
  }
  </code></pre>
  
  <p>On both servers I have latest versions of NestJS. Docker version is 19.03.5.</p>