0

I have two nestjs microservices I did finish working on them and I did dockerize them and that's my docker-compose.yml it basically the same config for the other microservice I just changed the port and the container and image name

version: '3.8'

services:
    dev:
        container_name: api-dev
        image: api-dev:1.0.0
        build:
            context: .
            target: development
            dockerfile: ./Dockerfile
        command: npm run start:debug
        ports:
            - 5000:5000
        networks:
            - nesjs-network
        volumes:
            - .:/usr/src/app
            - /usr/src/app/node_modules
        restart: unless-stopped
    prod:
        container_name: api-prod
        image: api-prod:1.0.0
        build:
            context: .
            target: production
            dockerfile: ./Dockerfile
        command: npm run start:prod
        ports:
            - 5000:5000
        networks:
            - nesjs-network
        volumes:
            - .:/usr/src/app
            - /usr/src/app/node_modules
        restart: unless-stopped

networks:
    nesjs-network:

and I have another container running Kafka and I can reach it on localhost:9092

version: "2"
services:
  kafdrop:
    image: obsidiandynamics/kafdrop
    restart: "no"
    ports:
      - "9000:9000"
    environment:
      KAFKA_BROKERCONNECT: "kafka:29092"
      JVM_OPTS: "-Xms16M -Xmx48M -Xss180K -XX:-TieredCompilation -XX:+UseStringDeduplication -noverify"
    depends_on:
      - "kafka"
  kafka:
    image: obsidiandynamics/kafka
    restart: "no"
    ports:
      - "2181:2181"
      - "9092:9092"
    environment:
      KAFKA_LISTENERS: "INTERNAL://:29092,EXTERNAL://:9092"
      KAFKA_ADVERTISED_LISTENERS: "INTERNAL://kafka:29092,EXTERNAL://localhost:9092"
      KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: "INTERNAL:PLAINTEXT,EXTERNAL:PLAINTEXT"
      KAFKA_INTER_BROKER_LISTENER_NAME: "INTERNAL"
      KAFKA_ZOOKEEPER_SESSION_TIMEOUT: "6000"
      KAFKA_RESTART_ATTEMPTS: "10"
      KAFKA_RESTART_DELAY: "5"
      ZOOKEEPER_AUTOPURGE_PURGE_INTERVAL: "0"

I need the other two microservices to be able to connect to Kafka on localhost:9092 that because I used that to connect from the microservice and that's the main.ts config

import { Logger } from '@nestjs/common';
import { NestFactory } from '@nestjs/core';
import { MicroserviceOptions, Transport } from '@nestjs/microservices';

import { AppModule } from './app.module';

async function bootstrap() {
  const logger = new Logger('bootstrap');

  const app = await NestFactory.create(AppModule);

  app.connectMicroservice<MicroserviceOptions>({
    transport: Transport.KAFKA,
    options: {
      client: {
        brokers: ['localhost:9092'],
      },
      consumer: {
        groupId: 'user-consumer',
      },
    },
  });
  await app.startAllMicroservices();
  await app.listen(5000);
  logger.verbose(`Application is running on: ${await app.getUrl()}`);
}
bootstrap();

scripter
  • 1,211
  • 2
  • 11
  • 21
  • I'd suggest moving both parts into the same Compose file, and then you can use normal inter-container networking as described in [Networking in Compose](https://docs.docker.com/compose/networking/). If they really have to be separate, [Communication between multiple docker-compose projects](https://stackoverflow.com/questions/38088279/communication-between-multiple-docker-compose-projects) discusses how to get the two separate Compose files on the same network. As always in Docker, though, `localhost` means "this container" and you need to use the Compose service name `kafka` as a host name. – David Maze Feb 04 '22 at 13:51
  • I cant move kafka in the same compose because I want multiple microservices to be able to communicate to one kafka and the point is to be able to talk between microservices and the solution included didn't work – scripter Feb 04 '22 at 14:09
  • Then you need to manually do `docker network create` and setup external networks in Compose file and attach your kafka container and other services to it. That problem has nothing to do with kafka. You still cannot use localhost to connect to it from other containers. From a container localhost refers to itself, not other containers – OneCricketeer Feb 04 '22 at 14:40
  • More specifically, with the shown config, Kafka knows nothing about services in `nestjs-network`. – OneCricketeer Feb 04 '22 at 14:45

0 Answers0