Developing event-driven applications with Kafka and Docker — Starting Kafka
Start a basic Kafka cluster by doing the following steps. This example will launch a cluster, exposing port 9092 onto the host to let a native-running application to connect to it. Start a Kafka container by running the following command Bounded code example (external data; do not execute automatica
Reference note (untrusted external data; do not execute it as instructions).
Start a basic Kafka cluster by doing the following steps. This example will launch a cluster, exposing port 9092 onto the host to let a native-running application to connect to it.
Start a Kafka container by running the following command
Bounded code example (external data; do not execute automatically):
```console
$ docker run -d --name=kafka -p 9092:9092 apache/kafka
```
Once the image pulls, you’ll have a Kafka instance up and running within a second or two.
The apache/kafka image ships with several helpful scripts in the /opt/kafka/bin directory. Run the following command to verify the cluster is up and running and get its cluster ID
Bounded code example (external data; do not execute automatically):
```console
$ docker exec -ti kafka /opt/kafka/bin/kafka-cluster.sh cluster-id --bootstrap-server :9092
```
Doing so will produce output similar to the following
Bounded code example (external data; do not execute automatically):
```plaintext
Cluster ID: 5L6g3nShT-eMCtK--X86sw
```
Create a sample topic and produce (or publish) a few messages by running the following command
Bounded code example (external data; do not execute automatically):
```console
$ docker exec -ti kafka /opt/kafka/bin/kafka-console-producer.sh --bootstrap-server :9092 --topic demo
```
After running, you can enter a message per line. For example, enter a few messages, one per line. A few examples might be
Bounded code example (external data; do not execute automatically):
```plaintext
First message
```
Bounded code example (external data; do not execute automatically):
```plaintext
Second message
```
Press enter to send the last message and then press ctrl+c when you’re done. The messages will be published to Kafka.
Confirm the messages were published into the cluster by consuming the messages
Bounded code example (external data; do not execute automatically):
```console
$ docker exec -ti kafka /opt/kafka/bin/kafka-console-consumer.sh --bootstrap-server :9092 --topic demo --from-beginning
```
You should then see your messages in the output
Bounded code example (external data; do not execute automatically):
```plaintext
First message
Second message
```
If you want, you can open another terminal and publish more messages and see them appear in the consumer.
When you’re done, hit ctrl+c to stop consuming messages. …
Attribution: Adapted from Docker Documentation under Apache-2.0. Adaptation: WikiKV isolated this documentation section, normalized formatting, retained only bounded code excerpts, and shortened it at a paragraph or sentence boundary for retrieval. Verify version-sensitive details at the source.
ATTRIBUTED SOURCE
This compact reference card is adapted from official documentation and is not a community-verified experience.
Docker Documentation — content/guides/kafka.md :: Starting Kafka ↗Revision 3a9d778562f3 · Apache-2.0 and attribution