Get started Kafka with Docker

Zookeeper 1

docker run -it --rm \
    -e ALLOW_ANONYMOUS_LOGIN=yes \
    -p 2181:2181 \
    -p 2888:2888 \
    -p 3888:3888 \
    -p 8080:8080 \
    -v /tmp/host/zookeeper:/bitnami/zookeeper
    bitnami/zookeeper

Kafka 2

docker run -it --rm \
    -p 9092:9092 \
    -e KAFKA_ADVERTISED_LISTENERS=PLAINTEXT://127.0.0.1:9092 \
    -e KAFKA_LISTENERS=PLAINTEXT://:9092 \
    -e KAFKA_ZOOKEEPER_CONNECT=127.0.0.1:2181 \
    -e ALLOW_PLAINTEXT_LISTENER=yes \
    -v /tmp/host/kafka:/bitnami/kafka
    bitnami/kafka

Testing 3

ches/kafka can provide tools4. And source code is in github5.

Create topic

docker run \
--rm ches/kafka kafka-topics.sh \
--create \
--topic test_topic \
--replication-factor 1 \
--partitions 1 \
--zookeeper 127.0.0.1:2181

List topic

docker run \
--rm ches/kafka kafka-topics.sh \
--list \
--zookeeper 127.0.0.1:2181

producer

docker run --rm --interactive \
ches/kafka kafka-console-producer.sh \
--topic test_topic \
--broker-list 127.0.0.1:9092

consumer

docker run --rm \
ches/kafka kafka-console-consumer.sh \
--topic test_topic \
--from-beginning \
--zookeeper 127.0.0.1:2181