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, andelasticsearchmodules 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
How to Cache Testcontainers Images in CIReduce Testcontainers pull time in CI by caching image tarballs with actions/cache and docker save/load, or b…
How to Use Testcontainers Cloud in CIOffload Testcontainers container execution to Testcontainers Cloud from CI using the setup action and a token…
How to Pin Testcontainers Images for Reproducible CIPin Testcontainers images to a specific tag or digest so CI runs stay reproducible, and configure asCompatibl…