Here are my two cents, since I had a hard time figuring this one out.
My $KAFKA_HOME/config/server.properties contains the following:
listener.security.protocol.map=INSIDE:PLAINTEXT,OUTSIDE:PLAINTEXT
advertised.listeners=INSIDE://${container_ip}:9092,OUTSIDE://${outside_host_ip}:29092
listeners=INSIDE://:9092,OUTSIDE://:29092
inter.broker.listener.name=INSIDE
This is creating two connections, one to be used inside docker and another to be used outside. You have to choose a new port for the latter, in my case 29092, make sure this port is exposed and mapped by docker.
I was not yet able to figure out a solution without the ${outside_host_ip} in the environment, hence I'm providing the host machine's ip as an env var.
Test:
- Enter the Kafka container and create a topic:
./kafka-topics.sh -zookeeper zookeeper:2181 --create --topic dummytopic --partitions 1 --replication-factor 1
- From outside the Kafka container do:
./kafka-console-producer.sh --broker-list 0.0.0.0:29092 --topic dummytopic
and input a message
I hope this helps others