Unable to connect to Kafka run in container from Spring Boot app run outside container
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
When deploying Apache Kafka in a Docker container and attempting to interact with it from a Spring Boot application running outside of the container, several configuration steps must be precisely managed to ensure successful communication. Below, we delve into why this configuration can be complex and provide a detailed guide on how to set it up correctly.
Understanding Kafka and Docker Network
Apache Kafka is a distributed streaming platform capable of handling trillions of events a day. Docker provides a way to package your application into a container making it easily deployable anywhere Docker is supported. However, one of the common challenges when integrating Dockerized services with non-Dockerized services arises frpm network connectivity issues.
By default, Docker containers run on a separate network. If you're running Kafka in Docker and a Spring Boot app natively on the host or on a different Docker network, ensuring proper network setup is critical for connectivity.
Adjusting Kafka Configuration
To connect from a Spring Boot application to a Kafka instance running inside a Docker container, follow these configuration steps:
- Kafka Docker ConfigurationEnsure Kafka advertised listeners are configured correctly.
ADVERTISED_LISTENERSis the key setting here, which tells clients how they can connect to Kafka.
INSIDEandOUTSIDEare custom names representing different networking interfaces.localhost:9094is the port exposed on the host machine where external applications can connect to Kafka.
- Expose Necessary PortsMake sure to expose the
OUTSIDEport (9094 in this example) in your Docker command ordocker-compose.ymlfile.
- Spring Boot ConfigurationConfigure the Spring Boot application to use the appropriate bootstrap servers:
This configuration lets the Spring Boot app know where to send and receive messages from Kafka.
Network Troubleshooting
If there are any issues in connecting to Kafka, consider the following:
- Firewall and Security Rules: Ensure that no firewall or security settings are blocking the connections.
- Correct IP Address: When running on different machines or cloud setups, ensure you're using the correct accessible IP address instead of
localhost.
Common Errors and Solutions
| Error | Cause | Solution |
| Timeout Connecting to Broker | Wrong Kafka listener configuration. | Check KAFKA_ADVERTISED_LISTENERS. |
| Connection Refused | Incorrect port exposed or firewall issue. | Verify Docker port bindings and firewall settings. |
| No Brokers Available | Spring Boot configured with wrong port. | Ensure spring.kafka.bootstrap-servers matches the advertised port. |
Practical Testing
To ensure your configuration works, you can set up a simple producer and consumer within your Spring Boot application to send and receive messages.
By deploying these configurations properly, you can effectively communicate between a Spring Boot application and a Kafka instance running in Docker. This setup is crucial for development environments where components might be scattered across different services or during migration phases of project environments.

