Not able to create kafka topic using docker-compose
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
When topic creation fails in a Docker Compose Kafka setup, the problem is usually not Docker Compose itself. It is almost always one of four things: the broker is not ready yet, the listener configuration is wrong, the command is targeting the wrong hostname or port, or the requested replication settings do not match the number of brokers you actually started.
Start With a Minimal Working Setup
A single-broker local setup should use a single replication factor. If you ask Kafka for a replication factor of 3 while running one broker, topic creation will fail.
A simple Compose example for local development looks like this:
The important part is consistency between listeners, advertised listeners, and the client command you use.
Create the Topic After the Broker Is Ready
Even if the container is running, Kafka may not be ready to accept admin commands yet. That is why commands executed immediately after docker compose up often fail.
Create the topic from inside the container using the broker's internal address.
For a single container, kafka:9092 is usually the correct internal bootstrap address. From your host machine, the advertised external address is often localhost:9092.
Common Failure Modes
Wrong Bootstrap Server
If you run the command inside the container, do not use localhost:9092 unless the broker is באמת listening there in that container context. Inside Docker networking, the service name is often the right host.
Wrong Replication Factor
One broker supports replication factor 1. Asking for 2 or 3 on a one-broker stack produces an error.
Bad advertised.listeners
Kafka returns broker addresses to clients. If those addresses are wrong for the environment where the client is running, admin operations fail even though the port looks open.
Running Too Early
The broker process may still be formatting storage, electing a controller, or starting listeners. depends_on starts containers in order, but it does not guarantee Kafka is ready for commands.
A Reliable Readiness Pattern
The safest approach is to wait for the broker to respond before creating the topic.
This is more reliable than firing the create command immediately after container startup.
Auto-Creation Versus Explicit Creation
Some images and broker settings allow automatic topic creation, but relying on it is often confusing in development because it hides errors in naming and configuration. Explicit admin commands are clearer.
If you use an image-specific environment variable such as KAFKA_CREATE_TOPICS, verify that the image actually supports it. Different Kafka images expose different helper behaviors.
Check the Broker Logs
If topic creation still fails, inspect logs first.
Look for listener binding errors, advertised listener mismatches, controller startup issues, or storage formatting problems.
Common Pitfalls
A common mistake is creating a topic with replication factor 3 on a one-broker local setup.
Another mistake is using localhost:9092 from inside the Kafka container. Inside Docker networking, the broker is often reachable as kafka:9092 instead.
Developers also assume depends_on means Kafka is ready for admin commands. It only means the container was started, not that the broker finished booting.
Summary
- Topic creation failures in Compose are usually listener, readiness, or replication-factor problems.
- Use replication factor
1for a single local broker. - Run admin commands against the correct bootstrap address for the environment.
- Wait until the broker is actually ready before creating topics.
- Check image-specific behavior and broker logs when auto-creation does not work.
Related reading
- Not able to delete topics from Kafka
- Not able to start kafka with .\bin\windows\kafka-server-start.bat .\config\server.properties cmd
- Not able to Start rabbitmq server in centos 7 using systemctl
- Not clear about the meaning of auto.offset.reset and enable.auto.commit in Kafka
- Not Able To Create Pod in Kubernetes
- npm ERR Tracker idealTree already exists while creating the Docker image for Node project
- Not able to import the spark packages
- Not able to load weights for fine tuning in Keras with ResNet50

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.