{"record":{"id":"38550df0c0ba0dd6","repo":"testcontainers/testcontainers-java","slug":"cannot-determine-elasticsearch-dns-name-when-using-a-custom","errorCode":null,"errorMessage":"Cannot determine Elasticsearch DNS name. When using a custom network, set a network alias on the Elasticsearch container.","messagePattern":"Cannot determine Elasticsearch DNS name\\. When using a custom network, set a network alias on the Elasticsearch container\\.","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"modules/elasticsearch/src/main/java/org/testcontainers/elasticsearch/KibanaContainer.java","lineNumber":459,"sourceCode":"                    String cleaned = alias.trim();\n                    // Skip Testcontainers auto-generated aliases (tc-*), prefer user-defined ones\n                    if (!cleaned.startsWith(\"tc-\")) {\n                        log.info(\"Using Elasticsearch network alias: {}\", cleaned);\n                        return cleaned;\n                    }\n                }\n            }\n        }\n\n        // Fallback: use container name\n        String containerName = info.getName();\n        if (containerName != null && !containerName.trim().isEmpty()) {\n            String dnsName = containerName.replaceFirst(\"^/\", \"\").trim();\n            log.info(\"No user-defined network alias found, using Elasticsearch container name: {}\", dnsName);\n            return dnsName;\n        }\n\n        throw new IllegalStateException(\n            \"Cannot determine Elasticsearch DNS name. \" +\n            \"When using a custom network, set a network alias on the Elasticsearch container.\"\n        );\n    }\n\n    private ContainerNetwork findNetworkEndpoint(Map<String, ContainerNetwork> networks, Network network) {\n        // Try network ID first\n        ContainerNetwork endpoint = networks.get(network.getId());\n        if (endpoint != null) {\n            return endpoint;\n        }\n\n        // Try network name as fallback (Docker may key by name instead of ID)\n        try {\n            String networkName = DockerClientFactory\n                .instance()\n                .client()\n                .inspectNetworkCmd()","sourceCodeStart":441,"sourceCodeEnd":477,"githubUrl":"https://github.com/testcontainers/testcontainers-java/blob/8e549514e3f01c57d70546fbb8599d138f3903e5/modules/elasticsearch/src/main/java/org/testcontainers/elasticsearch/KibanaContainer.java#L441-L477","documentation":"Thrown by resolveExistingEsDnsNameOnNetwork as the last resort when no user-defined network alias and no usable container name can be found for the Elasticsearch container on the shared network. Kibana needs some DNS-resolvable name (alias or container name) to address Elasticsearch and cannot determine one.","triggerScenarios":"Managed mode on a custom network where the ES container has no network alias set via withNetworkAliases(...) and its container name is null/blank.","commonSituations":"Using a manually created Network without aliases; stripping container names with a naming strategy that yields empty names; upgrading Testcontainers where default aliases were no longer auto-applied.","solutions":["Set an alias on the Elasticsearch container: elasticsearch.withNetworkAliases(\"elasticsearch\").","Ensure the ES container has a non-empty container name (withCreateContainerCmdModifier cmd -> cmd.withName(\"elasticsearch\")).","Let KibanaContainer create the network itself by removing explicit network configuration from both containers."],"exampleFix":"// before\nNetwork net = Network.newNetwork();\nelasticsearch.withNetwork(net); // no alias\nkibana.withNetwork(net);\n// after\nNetwork net = Network.newNetwork();\nelasticsearch.withNetwork(net).withNetworkAliases(\"elasticsearch\");\nkibana.withNetwork(net);","handlingStrategy":"fallback","validationCode":"elasticsearch.withNetwork(net).withNetworkAliases(\"elasticsearch\"); // alias guarantees DNS name","typeGuard":"boolean hasDnsName(ElasticsearchContainer es) { return es.getNetworkAliases().stream().anyMatch(a -> !a.startsWith(\"tc-\")) || es.getContainerName() != null; }","tryCatchPattern":"try { kibana.start(); } catch (IllegalStateException e) { if (e.getMessage().contains(\"Cannot determine Elasticsearch DNS name\")) throw new ConfigException(\"Set withNetworkAliases(\\\"elasticsearch\\\") on the ES container\"); throw e; }","preventionTips":["Always set withNetworkAliases on the Elasticsearch container when using a custom network","Avoid container naming schemes that produce empty names","Let KibanaContainer manage the network if you don't need custom DNS"],"tags":["network","dns","elasticsearch","testcontainers"],"backgroundTag":"resource-not-found","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"}