I am new to docker and I'm having a problem running a Kafka broker inside a Docker container. I developed spring boot application with kafka,producer and consumer in the same application . application running successfully without docker , but when I try to run it through docker container on my host machine give me this error:
org.apache.kafka.clients.NetworkClient : [Producer clientId=producer-1] Connection to node -1 (localhost/127.0.0.1:9092) could not be established. Broker may not be available.
org.apache.kafka.clients.NetworkClient : [Consumer clientId=consumer-group_id-1, groupId=group_id] Bootstrap broker localhost:9092 (id: -1 rack: null) disconnected
I used spring boot 2.4.2 and kafka_2.7.0
Steps I followed:
1-I build docker image for spring boot application.
2-I create docker-compose.yml to run zookeeper and kafka services as following:
version: '3'
services:
zookeeper:
image: wurstmeister/zookeeper
ports:
- "2181:2181"
kafka:
image: wurstmeister/kafka
build:
context: .
dockerfile: Dockerfile
ports:
- "9092:9092"
environment:
KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://localhost:9092
KAFKA_LISTENERS: PLAINTEXT://0.0.0.0:9092
KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
KAFKA_MESSAGE_MAX_BYTES: 2000000
KAFKA_CREATE_TOPICS: "Topic1:1:1"
volumes:
- /var/run/docker.sock:/var/run/docker.sock
depends_on:
- zookeeper
3- Then I execute the docker-compose.yml: docker-compose up -d
4- Finally I run spring boot app image: docker run -p 8080:8080 spring-boot-app .
This is my client config(spring boot app): application.properties:
Consumer configuration
#spring.kafka.consumer.bootstrap-servers=localhost:9092
spring.kafka.consumer.group-id==group_id
spring.kafka.consumer.auto-offset-reset=earliest
spring.kafka.consumer.key-deserializer=
org.apache.kafka.common.serialization.StringDeserializer
spring.kafka.consumer.value-deserializer= org.apache.kafka.common.serialization.StringDeserializer
Producer configuration
#spring.kafka.producer.bootstrap-servers=localhost:9092
spring.kafka.producer.key-serializer= org.apache.kafka.common.serialization.StringSerializer
spring.kafka.producer.value-serializer= org.apache.kafka.common.serialization.StringSerializer
Read more here: https://stackoverflow.com/questions/66296686/broker-not-available-when-i-run-spring-boot-application-with-kafka-inside-docker
Content Attribution
This content was originally published by Mohammed Mostafa at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.