Skip to content

Kafka Containers

Testcontainers can be used to automatically instantiate and manage Apache Kafka containers. More precisely Testcontainers uses the official Docker images for Confluent OSS Platform

Benefits

  • Running a single node Kafka installation with just one line of code
  • No need to manage external Zookeeper installation, required by Kafka. But see below

Example

Create a KafkaContainer to use it in your tests:

KafkaContainer kafka = new KafkaContainer(DockerImageName.parse("confluentinc/cp-kafka:6.2.1"))

The correspondence between Confluent Platform versions and Kafka versions can be seen in Confluent documentation

Now your tests or any other process running on your machine can get access to running Kafka broker by using the following bootstrap server location:

kafka.getBootstrapServers()

Options

Using external Zookeeper

If for some reason you want to use an externally running Zookeeper, then just pass its location during construction:

KafkaContainer kafka = new KafkaContainer(DockerImageName.parse("confluentinc/cp-kafka:6.2.1"))
    .withNetwork(network)
    .withExternalZookeeper("zookeeper:2181");

Using Kraft mode

KRaft mode was declared production ready in 3.3.1 (confluentinc/cp-kafka:7.3.x)"

KafkaContainer kafka = new KafkaContainer(DockerImageName.parse("confluentinc/cp-kafka:7.4.0")).withKraft()

See the versions interoperability matrix for more details.

Register listeners

There are scenarios where additional listeners are needed because the consumer/producer can be in another container in the same network or a different process where the port to connect differs from the default exposed port 9093. E.g Toxiproxy.

KafkaContainer kafka = new KafkaContainer(KAFKA_KRAFT_TEST_IMAGE)
    .withListener(() -> "kafka:19092")
    .withNetwork(network);

Container defined in the same network:

GenericContainer<?> kcat = new GenericContainer<>("confluentinc/cp-kcat:7.4.1")
    .withCreateContainerCmdModifier(cmd -> {
        cmd.withEntrypoint("sh");
    })
    .withCopyToContainer(Transferable.of("Message produced by kcat"), "/data/msgs.txt")
    .withNetwork(network)
    .withCommand("-c", "tail -f /dev/null")

Client using the new registered listener:

kcat.execInContainer("kcat", "-b", "kafka:19092", "-t", "msgs", "-P", "-l", "/data/msgs.txt");
String stdout = kcat
    .execInContainer("kcat", "-b", "kafka:19092", "-C", "-t", "msgs", "-c", "1")
    .getStdout();

Adding this module to your project dependencies

Add the following dependency to your pom.xml/build.gradle file:

testImplementation "org.testcontainers:kafka:1.19.0"
<dependency>
    <groupId>org.testcontainers</groupId>
    <artifactId>kafka</artifactId>
    <version>1.19.0</version>
    <scope>test</scope>
</dependency>