Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ ARG kafkaversion=3.6.1
ARG scalaversion=2.13

ENV KRAFT_CONTAINER_HOST_NAME=
ENV KRAFT_ADVERTISE_HOST_NAME=
ENV KRAFT_CREATE_TOPICS=
ENV KRAFT_PARTITIONS_PER_TOPIC=
ENV KRAFT_AUTO_CREATE_TOPICS=
Expand Down
36 changes: 30 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,29 @@ $ docker run -e KRAFT_CONTAINER_HOST_NAME=kafka -e KRAFT_CREATE_TOPICS=topic-a,t
- 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`.

### Access Across The Network

You can expose this Kafka broker to the network, in case any non-container applications need to access it or you want to access it from other machines.
This can also be useful if you run Linux in a VM (or via WSL) and want to allow your host machine to access Kafka.

Remember that this image **does not use Kafka authentication** so you should only do this on networks you trust.

```bash
$ hostname
my-linux-box
$ docker run -p 9093:9093 -e KRAFT_ADVERTISE_HOST_NAME=my-linux-box -e KRAFT_CONTAINER_HOST_NAME=kafka -e KRAFT_PARTITIONS_PER_TOPIC=3 -e KRAFT_AUTO_CREATE_TOPICS=true moeenz/docker-kafka-kraft
```

This allows other machines on the network to connect to Kafka at `my-linux-box:9093`.
`KRAFT_ADVERTISE_HOST_NAME` can also be an IP address if your network is not setup for hostnames:

```bash
$ ip addr show eth0
...
inet 192.168.0.2
$ docker run -p 9093:9093 -e KRAFT_ADVERTISE_HOST_NAME=192.168.0.2 -e KRAFT_CONTAINER_HOST_NAME=kafka -e KRAFT_PARTITIONS_PER_TOPIC=3 -e KRAFT_AUTO_CREATE_TOPICS=true moeenz/docker-kafka-kraft
```

### Compose Example

```yaml
Expand All @@ -35,12 +58,13 @@ services:

## Environment Variables

| Name | Type | Description | Example |
| -------------------------- | -------- | -------------------------------------------------------------- | ----------------------- |
| 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 |
| Name | Type | Description | Example |
| -------------------------- | -------- | --------------------------------------------------------------------------------- | ----------------------- |
| KRAFT_CONTAINER_HOST_NAME | string | Hostname for the running container as the Kafka listener | kafka |
| KRAFT_ADVERTISE_HOST_NAME | string | Hostname or IP address that Kafka advertises externally. Defaults to `localhost`. | my-hostname |
| 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
10 changes: 7 additions & 3 deletions docker-entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,18 @@ else
echo "auto.create.topics.enable=${KRAFT_AUTO_CREATE_TOPICS}" >> $properties_file;
fi

if [ -z $KRAFT_ADVERTISE_HOST_NAME ]; then
KRAFT_ADVERTISE_HOST_NAME=localhost
fi

if [ -z $KRAFT_CONTAINER_HOST_NAME ]; then
echo "listeners=CONTROLLER://:19092,EXTERNAL://:9093" >> $properties_file;
echo "advertised.listeners=EXTERNAL://localhost:9093" >> $properties_file;
echo "advertised.listeners=EXTERNAL://${KRAFT_ADVERTISE_HOST_NAME}:9093" >> $properties_file;
echo "inter.broker.listener.name=EXTERNAL" >> $properties_file;
echo "listener.security.protocol.map=CONTROLLER:PLAINTEXT,EXTERNAL:PLAINTEXT" >> $properties_file;
else
echo "listeners=CONTROLLER://:19092,INTERNAL://:9092,EXTERNAL://:9093" >> $properties_file;
echo "advertised.listeners=INTERNAL://${KRAFT_CONTAINER_HOST_NAME}:9092,EXTERNAL://localhost:9093" >> $properties_file;
echo "advertised.listeners=INTERNAL://${KRAFT_CONTAINER_HOST_NAME}:9092,EXTERNAL://${KRAFT_ADVERTISE_HOST_NAME}:9093" >> $properties_file;
echo "inter.broker.listener.name=EXTERNAL" >> $properties_file;
echo "listener.security.protocol.map=CONTROLLER:PLAINTEXT,INTERNAL:PLAINTEXT,EXTERNAL:PLAINTEXT" >> $properties_file;
fi
Expand Down Expand Up @@ -62,5 +66,5 @@ else
echo "==> ✅ Requested topics created.";
fi


echo "==> Connect to Kafka at ${KRAFT_ADVERTISE_HOST_NAME}:9093"
wait "$child";