Provides functionality for creating and managing Kafka container configurations.
This implementation uses the official apache/kafka Docker image which runs in KRaft mode
by default (no Zookeeper required). This makes Kafka deployment significantly simpler.
Example
config = KafkaContainer.new()
{:ok, container} = Testcontainers.start_container(config)
# Get the bootstrap server address
bootstrap_servers = KafkaContainer.bootstrap_servers(container)With automatic topic creation
config =
KafkaContainer.new()
|> KafkaContainer.with_topics(["my-topic", "other-topic"])
{:ok, container} = Testcontainers.start_container(config)Note on Port Binding
This implementation uses a randomly selected fixed host port (between 29000-29999) for the Kafka listener. This is necessary because the apache/kafka image requires knowing the advertised listener address at startup time, before the container's dynamic port mapping is known.
If you need to use a specific port, you can set it with with_kafka_port/2.
Summary
Functions
Returns the bootstrap servers string for connecting to the Kafka container.
Creates a new KafkaContainer struct with default configurations.
Returns the port on the host machine where the Kafka container is listening.
Overrides the default cluster id used for the Kafka container.
Overrides the default controller port used for the Kafka container.
Overrides the default image used for the Kafka container.
Overrides the host port used for the Kafka container.
Overrides the default node id used for the Kafka container.
Set the reuse flag to reuse the container if it is already running.
Sets the topics to be created automatically when the container starts.
Overrides the default wait timeout used for the Kafka container.
Functions
Returns the bootstrap servers string for connecting to the Kafka container.
Creates a new KafkaContainer struct with default configurations.
A random port between 29000-29999 is selected for the Kafka listener.
Returns the port on the host machine where the Kafka container is listening.
Overrides the default cluster id used for the Kafka container.
Overrides the default controller port used for the Kafka container.
Overrides the default image used for the Kafka container.
Overrides the host port used for the Kafka container.
This port will be used on the host machine and also as the advertised listener port.
Overrides the default node id used for the Kafka container.
Set the reuse flag to reuse the container if it is already running.
Sets the topics to be created automatically when the container starts.
Example
config =
KafkaContainer.new()
|> KafkaContainer.with_topics(["my-topic", "other-topic"])
Overrides the default wait timeout used for the Kafka container.