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

  1. 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).
  2. Verify the KibanaContainer was built with the actual started ElasticsearchContainer instance, not a second, unstarted instance.
  3. 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

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


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)