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