Skip to content

Commit

Permalink
Expose an env variable to allow the auto creation of topics to be tur…
Browse files Browse the repository at this point in the history
…ned off to more closely resemble production behavior.
  • Loading branch information
Eric Lacker authored and moeenz committed Mar 6, 2024
1 parent b8d9574 commit e4754bf
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 1 deletion.
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ ARG scalaversion=2.13
ENV KRAFT_CONTAINER_HOST_NAME=
ENV KRAFT_CREATE_TOPICS=
ENV KRAFT_PARTITIONS_PER_TOPIC=
ENV KRAFT_AUTO_CREATE_TOPICS=

RUN apk --no-cache add curl bash

Expand Down
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ Apache Kafka Docker image using using Kafka Raft metadata mode (KRaft). In KRaft

```bash
$ docker pull moeenz/docker-kafka-kraft
$ docker run -e KRAFT_CONTAINER_HOST_NAME=kafka -e KRAFT_CREATE_TOPICS=topic-a,topic-b,topic-c -e KRAFT_PARTITIONS_PER_TOPIC=3 moeenz/docker-kafka-kraft
$ docker run -e KRAFT_CONTAINER_HOST_NAME=kafka -e KRAFT_CREATE_TOPICS=topic-a,topic-b,topic-c -e KRAFT_PARTITIONS_PER_TOPIC=3 -e KRAFT_AUTO_CREATE_TOPICS=true moeenz/docker-kafka-kraft
```

- Now you can reach the container at `localhost:9093` on your host machine or inside Docker network with hostname `kafka`.
- Comma seperated values received by `KRAFT_CREATE_TOPICS` env will be used to create topics at startup time.
- The `KRAFT_AUTO_CREATE_TOPICS` env variable lets you turn off auto topic creation (`auto.create.topics.enable=false`). Default Kafka behavior is `true`.

### Compose Example

Expand All @@ -29,6 +30,7 @@ services:
- KRAFT_CONTAINER_HOST_NAME=kafka
- KRAFT_CREATE_TOPICS=topic-a,topic-b,topic-c
- KRAFT_PARTITIONS_PER_TOPIC=3
- KRAFT_AUTO_CREATE_TOPICS=true
```

## Environment Variables
Expand All @@ -38,6 +40,7 @@ services:
| KRAFT_CONTAINER_HOST_NAME | string | Hostname for the running container as the Kafka listener | kafka |
| KRAFT_CREATE_TOPICS | []string | Comma separated list of topics to be created post server setup | topic-a,topic-b,topic-c |
| KRAFT_PARTITIONS_PER_TOPIC | int | Number of partitions per topic | 3 |
| KRAFT_AUTO_CREATE_TOPICS | string | `true` or `false`, Kafka default behavior is `true`. | false |

## Resources

Expand Down
6 changes: 6 additions & 0 deletions docker-entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ kafka_addr=localhost:9093;

echo "==> Applying environment variables...";

if [ -z $KRAFT_AUTO_CREATE_TOPICS ]; then
echo "==> Not setting auto.create.topics.enable (default; environment variable not set)."
else
echo "==> Setting auto.create.topics.enable=${KRAFT_AUTO_CREATE_TOPICS}...";
echo "auto.create.topics.enable=${KRAFT_AUTO_CREATE_TOPICS}" >> $properties_file;
fi

if [ -z $KRAFT_CONTAINER_HOST_NAME ]; then
echo "listeners=CONTROLLER://:19092,EXTERNAL://:9093" >> $properties_file;
Expand Down
1 change: 1 addition & 0 deletions examples/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ services:
- KRAFT_CONTAINER_HOST_NAME=kafka
- KRAFT_CREATE_TOPICS=topic-a,topic-b,topic-c
- KRAFT_PARTITIONS_PER_TOPIC=3
- KRAFT_AUTO_CREATE_TOPICS=true

0 comments on commit e4754bf

Please sign in to comment.