Skip to content
Latchkey

How to Run Kafka, MongoDB, and Elasticsearch Containers in CI

The Kafka, MongoDB, and Elasticsearch modules each expose a helper (bootstrap servers, connection string, or HTTP host) tied to the mapped port.

Prefer the dedicated modules over raw images: KafkaContainer gives getBootstrapServers(), MongoDBContainer gives getConnectionString(), and ElasticsearchContainer gives getHttpHostAddress().

Steps

  • Add the kafka, mongodb, and elasticsearch modules you need.
  • Instantiate each container with a pinned image tag.
  • Read the helper method instead of building a URL by hand.
  • Wire the value into your client configuration.

Java example

StreamingIT.java
@Container
static KafkaContainer kafka =
    new KafkaContainer(DockerImageName.parse("confluentinc/cp-kafka:7.6.0"));

@Container
static MongoDBContainer mongo =
    new MongoDBContainer("mongo:7");

@Container
static ElasticsearchContainer es =
    new ElasticsearchContainer("docker.elastic.co/elasticsearch/elasticsearch:8.13.4");

// kafka.getBootstrapServers();
// mongo.getConnectionString();
// es.getHttpHostAddress();

Gotchas

  • Elasticsearch images are large; cache the layer or use Testcontainers Cloud to cut pull time.
  • The Kafka module wires listeners for you, so do not override the advertised listeners unless you must.

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →