1

docker-compose.yml

version: "3"
services:
  kafka:
    image: 'bitnami/kafka:latest'
    ports:
      - '9092:9092'
    environment:
      - KAFKA_ENABLE_KRAFT=yes
      - KAFKA_CFG_BROKER_ID=1
      - KAFKA_CFG_PROCESS_ROLES=broker,controller
      - KAFKA_CFG_CONTROLLER_LISTENER_NAMES=CONTROLLER
      - KAFKA_CFG_LISTENERS=PLAINTEXT://:9092,CONTROLLER://:9093
      - KAFKA_CFG_LISTENER_SECURITY_PROTOCOL_MAP=CONTROLLER:PLAINTEXT,PLAINTEXT:PLAINTEXT
      - KAFKA_CFG_ADVERTISED_LISTENERS=PLAINTEXT:/:9092
      - KAFKA_CFG_CONTROLLER_QUORUM_VOTERS=1@:9093
      - ALLOW_PLAINTEXT_LISTENER=yes

I can connect to it from other containers brought up by docker-compose using kafka:9092. From localhost, I can also connect using telnet: telnet localhost 9092 (any dummy string throws an exception in kafka, but the connection is there).

When I do try to connect from a kafka consumer running on localhost using localhost:9092, I am getting java.net.UnknownHostException: 1ffc30995c50: nodename nor servname provided, or not known. 1ffc30995c50 being the container id (and the hostname) of my kafka container.

As far as I understand KAFKA_CFG_ADVERTISED_LISTENERS is responsible for telling clients where to find the broker. In this case, it would have to return two different values ("kafka" to connect within the docker environment and "localhost" if outside) based on where the client connects from. Is that possible?

I hope that's clear and someone knows how to solve this. :)

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
kev
  • 8,928
  • 14
  • 61
  • 103
  • Does this answer your question? [Connect to Kafka running in Docker](https://stackoverflow.com/questions/51630260/connect-to-kafka-running-in-docker) – OneCricketeer May 27 '23 at 04:24

1 Answers1

2

I think I worked it out (using the try and error approach^^)

version: "3"
services:
  kafka:
    image: 'bitnami/kafka:latest'
    ports:
      - '9094:9094'

    environment:
      - KAFKA_ENABLE_KRAFT=yes
      - KAFKA_CFG_BROKER_ID=1
      - KAFKA_CFG_PROCESS_ROLES=broker,controller
      - KAFKA_CFG_CONTROLLER_LISTENER_NAMES=CONTROLLER
      - KAFKA_CFG_LISTENERS=PLAINTEXT://:9092,CONTROLLER://:9093,EXTERNAL://:9094
      - KAFKA_CFG_LISTENER_SECURITY_PROTOCOL_MAP=CONTROLLER:PLAINTEXT,PLAINTEXT:PLAINTEXT,EXTERNAL:PLAINTEXT
      - KAFKA_CFG_ADVERTISED_LISTENERS=PLAINTEXT://kafka:9092,EXTERNAL://localhost:9094
      - KAFKA_CFG_CONTROLLER_QUORUM_VOTERS=1@:9093
      - ALLOW_PLAINTEXT_LISTENER=yes

Connect from within the docker network: kafka:9092
Connect from outside (localhost): localhost:9094

kev
  • 8,928
  • 14
  • 61
  • 103