Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access "bitnami/kafka" docker container from everywhere

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. :)

like image 758
kev Avatar asked Oct 30 '25 02:10

kev


1 Answers

I think I worked it out (using the trial 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

like image 139
kev Avatar answered Oct 31 '25 20:10

kev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!