This module helps running elasticsearch using Testcontainers.
Note that it's based on the official Docker image provided by elastic.
You can start an elasticsearch container instance from any Java application by using:
HttpClient inside_block:httpClientContainer7 HttpClient with Elasticsearch 8 inside_block:httpClientContainer8 HttpClient with Elasticsearch 8 and SSL disabled inside_block:httpClientContainerNoSSL8 TransportClient inside_block:transportClientContainer
Note that if you are still using the TransportClient
(not recommended as it is deprecated), the default cluster name is set to docker-cluster so you need to change cluster.name setting
or set client.transport.ignore_cluster_name to true.
The default distribution of Elasticsearch comes with the basic license which contains security feature. You can turn on security by providing a password:
HttpClient inside_block:httpClientSecuredContainer
This module also provides a KibanaContainer for testing with Kibana.
Kibana requires a connection to Elasticsearch and KibanaContainer supports two modes: managed and external.
In managed mode, KibanaContainer automatically connects to an ElasticsearchContainer:
Kibana with Elasticsearch inside_block:managedModeCanStartAndReachElasticsearchInSameExplicitNetwork
When using managed mode with explicit networks, both containers must share the same Network instance.
Alternatively, you can omit the network configuration entirely, and KibanaContainer will do its best effort to create a shared, ad-hoc network automatically.
In external mode, KibanaContainer connects to an external Elasticsearch instance via URL and using provided credentials:
Kibana with external Elasticsearch inside_block:externalModeCanWorkWithUsernamePassword
For external mode with HTTPS, use withElasticsearchCaCertificate() to provide the CA certificate.
You can authenticate using either username/password (withElasticsearchCredentials()) or service account tokens (withElasticsearchServiceAccountToken()).
Add the following dependency to your pom.xml/build.gradle file:
=== "Gradle"
groovy testImplementation "org.testcontainers:testcontainers-elasticsearch:{{latest_version}}"
=== "Maven"
xml <dependency> <groupId>org.testcontainers</groupId> <artifactId>testcontainers-elasticsearch</artifactId> <version>{{latest_version}}</version> <scope>test</scope> </dependency>