I’ve been down this road before, and I can tell you from experience that the Confluent Kafka image suggested by Dave_17Sketch is indeed a solid choice. However, if you’re set on using wurstmeister/kafka, there’s a trick I’ve found that might help.
Instead of using KAFKA_CREATE_TOPICS, try creating a separate service for topic creation. Here’s what I mean:
This approach ensures that Kafka is fully up before attempting to create topics. As for connecting to your Spring app, double-check your application.properties. Make sure you’re using the correct bootstrap servers address (localhost:9092 if running on the same machine).
I’ve been wrestling with Kafka setups in Docker Compose too, and I found a solution that’s been working well for me. Instead of using pre-built images, I’ve had success with a custom Dockerfile approach. Here’s what I do:
Create a Dockerfile for Kafka:
FROM openjdk:8-jre-alpine
RUN apk add --no-cache bash
ADD kafka_2.13-2.8.1.tgz /opt
ENV KAFKA_HOME /opt/kafka_2.13-2.8.1
COPY start-kafka.sh /usr/bin
CMD ["start-kafka.sh"]
This gives you more control over the Kafka setup. For topic creation, I use a separate init container that runs kafka-topics.sh after Kafka is up. It’s a bit more work upfront, but it’s been rock solid for my Spring projects.
I’ve had success using the Confluent Platform’s cp-kafka image. It’s well-maintained and integrates smoothly with Spring Boot apps. Here’s a streamlined setup:
For topic creation, I recommend using Kafka’s built-in auto-create feature. Set KAFKA_AUTO_CREATE_TOPICS_ENABLE=true in the environment. This approach has proven reliable in my projects, eliminating the need for separate topic creation steps.
I’ve encountered similar issues when setting up Kafka with Docker Compose. From my experience, the Confluent Kafka image tends to be more reliable and easier to configure. Here’s a snippet that’s worked well for me:
For topic creation, I’d recommend using a separate service or init container to run the kafka-topics command after Kafka is up. This approach has been more consistent in my projects.