testcontainers/testcontainers-java · error · IllegalStateException
Elasticsearch containerId is not available. In managed…
Error message
Elasticsearch containerId is not available. In managed mode, Elasticsearch must be started via dependsOn(elasticsearch) before KibanaContainer is started.
What it means
KibanaContainer in managed mode requires an already-started Elasticsearch container. requireElasticsearchContainerId calls elasticsearch.getContainerId() and throws IllegalStateException when the id is blank, i.e. the Elasticsearch container object exists but has not been started yet. The library throws this instead of silently connecting Kibana to a nonexistent ES instance.
Solutions
- Start the ElasticsearchContainer before using Kibana features: ensure elasticsearch.start() is invoked (directly or via @Testcontainers/@Container) and that KibanaContainer is declared with dependsOn(elasticsearch).
- Verify the KibanaContainer was built with the actual started ElasticsearchContainer instance, not a second, unstarted instance.
- Move any token()/ES-id access to after container startup (e.g. inside the test method or after both containers have started).
Example fix
// before KibanaContainer kibana = new KibanaContainer(DockerImageName.parse(...)); String token = kibana.token(); // ES not started // after @Container ElasticsearchContainer es = new ElasticsearchContainer(...); @Container KibanaContainer kibana = new KibanaContainer(es).dependsOn(es); // access kibana.token() only in the test body, after containers started
Defensive patterns
Strategy: validation
Validate before calling
// before using kibana features in managed mode
assert es.isRunning() : "Elasticsearch container must be started before Kibana usage";
String esId = es.getContainerId();
if (esId == null || esId.isBlank()) throw new IllegalStateException("Start ElasticsearchContainer first"); Prevention
- Use @Testcontainers/@Container annotations so startup order and lifecycle are managed for you.
- Always pair KibanaContainer with dependsOn(elasticsearch) in managed mode.
- Only call token()/config accessors after both containers report isRunning().
When it happens
Trigger: Calling kibana.token() (or any path through esId/requireElasticsearchContainerId) when KibanaContainer is in managed mode (constructed with a real ElasticsearchContainer) but that ElasticsearchContainer was never started, or was created but only referenced via dependsOn without being the started instance Kibana holds.
Common situations: Declaring new KibanaContainer(elasticsearch) but calling token()/config access before starting the containers; forgetting that dependsOn only orders lifecycle and does not itself start ES; accessing the token in a @BeforeEach that runs before container startup; accidentally constructing Kibana with a fresh ElasticsearchContainer that is never part of the test lifecycle.
Understand the failure class
Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.
Related errors
- withReuse(true) is not supported for KibanaContainer in…
- Cannot set Elasticsearch URL when using Elasticsearch…
- Failed to connect Elasticsearch container to ad-hoc shared…
- Cannot create service account token in external mode
- Failed to create Kibana service account token. Exit code
AI-assisted analysis of testcontainers/testcontainers-java@8e549514e3 (2026-09-12).
Data as JSON: /api/errors/d7d54805dac78b7b.
Report an issue: GitHub.
Appendix: source
Thrown at modules/elasticsearch/src/main/java/org/testcontainers/elasticsearch/KibanaContainer.java:494
.client()
.inspectNetworkCmd()
.withNetworkId(network.getId())
.exec()
.getName();
if (networkName != null) {
return networks.get(networkName);
}
} catch (Exception e) {
// Ignore and return null
}
return null;
}
private String requireElasticsearchContainerId() {
String id = elasticsearch.getContainerId();
if (StringUtils.isBlank(id)) {
throw new IllegalStateException(
"Elasticsearch containerId is not available. In managed mode, Elasticsearch must be started via dependsOn(elasticsearch) " +
"before KibanaContainer is started."
);
}
return id.trim();
}
private void connectRunningContainerToNetwork(String containerId, Network network) {
String networkId = network.getId();
try {
DockerClientFactory
.instance()
.client()
.connectToNetworkCmd()
.withContainerId(containerId)
.withNetworkId(networkId)
.exec();View on GitHub (pinned to 8e549514e3)