Kafka with Docker dynamic advertised_host_name
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Apache Kafka is a highly popular distributed event streaming platform capable of handling trillions of events a day. Initially conceived as a messaging queue, Kafka is based on an abstraction of a distributed commit log. When used in conjunction with Docker, a platform for developing, shipping, and running applications inside lightweight containers, Kafka offers significant flexibility, particularly in dynamic environments such as cloud-based infrastructures.
One important aspect to master when configuring Kafka with Docker is the handling of the advertised.listeners setting, which tells other brokers and clients how to connect to the current Kafka broker. Managing this setting becomes complex when dealing with dynamic environments like Docker where the IP address or hostname can change.
Understanding advertised.listeners
Kafka brokers are configured with a list of listeners (also called endpoints) that are used for intra-broker communication and to connect with clients. The advertised.listeners parameter is crucial as it defines the address of the Kafka broker that is advertised to other brokers and clients. In a Docker context, where the IP or hostname can be non-static, managing this configuration dynamically is essential.
Docker and Dynamic advertised.listeners
When deploying Kafka on Docker, especially on platforms like Kubernetes or Docker Swarm, the internal IP address of the container can change whenever the container restarts or when there is any orchestration action. Thus, hard-coding IP addresses or hostnames in your Kafka configuration isn't feasible.
To handle this, we need a method to dynamically set the advertised.listeners value. One common approach is to utilize environment variables or templating tools that configure this setting on container initialization. Here's a basic example of how you might achieve this using Docker Compose:
In this Docker Compose snippet, KAFKA_ADVERTISED_LISTENERS is dynamically set using a command to fetch the host IP. The HOSTNAME_COMMAND can be replaced or modified based on the specific needs or networking setup of your Docker environment.
Summary Table
| Parameter | Description | Example Value |
KAFKA_ZOOKEEPER_CONNECT | Connection string for Kafka to connect to Zookeeper. | zookeeper:2181 |
KAFKA_ADVERTISED_LISTENERS | The listeners Kafka advertises to the outside world. Can dynamically pick up the host IP. | INSIDE://:9092,OUTSIDE://_{HOSTNAME_COMMAND}:9094 |
KAFKA_LISTENER_SECURITY_PROTOCOL_MAP | Maps listener names to security protocols. | INSIDE:PLAINTEXT,OUTSIDE:PLAINTEXT |
KAFKA_INTER_BROKER_LISTENER_NAME | Preferred listener for communication between brokers in the same cluster. | INSIDE |
Troubleshooting Common Issues
Configuring advertised.listeners incorrectly can lead to issues where brokers may not be reachable. Always ensure that:
- The listener’s
networkmatches with Docker's network configurations. - Ports are correctly exposed and mapped in Docker settings.
- Security settings and authentication are correctly set if you’re using
SASL/SSLfor Kafka.
Conclusion
Deploying Kafka within Docker containers offers a scalable and flexible messaging solution, especially when combined with dynamic configuration management for advertised.listeners. As enterprises move towards microservices and cloud-native architectures, such integrations are becoming increasingly critical. Properly configuring dynamic settings ensures robust and responsive Kafka deployments, adaptable to various environments with minimal manual intervention.
Related reading
- Kafka with .Net Client
- Kafka with python How to send topic to postgreSQL?
- Kafka with Zookeeper 3.5.7 Crash NoSuchMethodError java.nio.ByteBuffer.flip()
- Kafka won't start with PEM certificate
- Kafka/Confluent ProducerConfig change default max.request.size using docker images
- KafkaListener in Unit test case does not consume from the container factory
- Kafka writes data directly on disk?
- Keeping consumer alive using Kafka

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.