{"record":{"id":"d8dc4908bca75a5b","repo":"testcontainers/testcontainers-java","slug":"cannot-set-elasticsearch-url-when-using-elasticsearch","errorCode":null,"errorMessage":"Cannot set Elasticsearch URL when using Elasticsearch container","messagePattern":"Cannot set Elasticsearch URL when using Elasticsearch container","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"modules/elasticsearch/src/main/java/org/testcontainers/elasticsearch/KibanaContainer.java","lineNumber":173,"sourceCode":"        if (reusable && elasticsearch != null) {\n            throw new IllegalStateException(\n                \"withReuse(true) is not supported for KibanaContainer in managed mode. \" +\n                \"Use external mode (withElasticsearchUrl) to enable reuse.\"\n            );\n        }\n        return super.withReuse(reusable);\n    }\n\n    /**\n     * Configures the Elasticsearch URL for external mode.\n     *\n     * @param elasticsearchUrl the Elasticsearch URL (e.g., \"https://my.fancy.setup.elastic.cloud:9200\")\n     * @return this container instance\n     * @throws IllegalStateException if already using managed mode\n     */\n    public KibanaContainer withElasticsearchUrl(String elasticsearchUrl) {\n        if (elasticsearch != null) {\n            throw new IllegalStateException(\"Cannot set Elasticsearch URL when using Elasticsearch container\");\n        }\n        this.elasticsearchUrl = elasticsearchUrl;\n        return this;\n    }\n\n    /**\n     * Configures Kibana to authenticate using the kibana_system user.\n     *\n     * @param password the password for the kibana_system user\n     * @return this container instance\n     */\n    public KibanaContainer withKibanaSystemPassword(String password) {\n        return withKibanaUsernameAndPassword(KIBANA_SYSTEM_USER, password);\n    }\n\n    /**\n     * Configures credentials Kibana will use for authentication.\n     *","sourceCodeStart":155,"sourceCodeEnd":191,"githubUrl":"https://github.com/testcontainers/testcontainers-java/blob/8e549514e3f01c57d70546fbb8599d138f3903e5/modules/elasticsearch/src/main/java/org/testcontainers/elasticsearch/KibanaContainer.java#L155-L191","documentation":"KibanaContainer supports two modes: managed (it launches its own Elasticsearch container) and external (it points at an Elasticsearch URL you supply). withElasticsearchUrl(String) throws IllegalStateException if an Elasticsearch container was already configured via withElasticsearch(...), because a Kibana container cannot target both.","triggerScenarios":"Calling withElasticsearchUrl(...) on a KibanaContainer that was constructed/configured with withElasticsearch(elasticsearchContainer) (or vice versa in the opposite order).","commonSituations":"Refactoring tests from managed to external mode and leaving the withElasticsearch(...) call in place; copying configuration from another test class.","solutions":["Remove the withElasticsearch(elasticsearchContainer) call if you want external mode.","Or remove the withElasticsearchUrl(...) call if you want managed mode.","Keep exactly one mode configuration per KibanaContainer instance."],"exampleFix":"// before\nnew KibanaContainer(IMAGE)\n    .withElasticsearch(esContainer)\n    .withElasticsearchUrl(\"http://es.local:9200\"); // throws\n// after\nnew KibanaContainer(IMAGE)\n    .withElasticsearchUrl(\"http://es.local:9200\");","handlingStrategy":"validation","validationCode":"assert !(usingManagedEs && externalEsUrl != null) : \"KibanaContainer supports either managed or external Elasticsearch, not both\";","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Choose managed vs external mode once, at builder construction.","Delete withElasticsearch(...) when migrating to withElasticsearchUrl(...).","Wrap container creation in a single factory method to enforce the invariant.","Review test base classes for leftover mode configuration."],"tags":["kibana","elasticsearch","testcontainers","conflicting-options"],"backgroundTag":"conflicting-config-options","analyzedSha":"8e549514e3f01c57d70546fbb8599d138f3903e5","analyzedAt":"2026-09-12T14:56:41.227Z","contentChangedAt":"2026-09-12T14:56:41.227Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}