{"record":{"id":"d04e16cc95fb635c","repo":"testcontainers/testcontainers-java","slug":"elasticsearch-container-is-not-connected-to-the-expected","errorCode":null,"errorMessage":"Elasticsearch container is not connected to the expected network. Ensure both containers use the same Network instance.","messagePattern":"Elasticsearch container is not connected to the expected network\\. Ensure both containers use the same Network instance\\.","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"modules/elasticsearch/src/main/java/org/testcontainers/elasticsearch/KibanaContainer.java","lineNumber":431,"sourceCode":"        // We don't need to provide an explicit alias - we'll use the container name for DNS resolution.\n        // Equivalent of https://docs.docker.com/reference/cli/docker/network/connect/\n        connectRunningContainerToNetwork(esId, createdSharedNetwork);\n    }\n\n    private String resolveExistingEsDnsNameOnNetwork(Network network) {\n        String esId = requireElasticsearchContainerId();\n\n        InspectContainerResponse info = DockerClientFactory.instance().client().inspectContainerCmd(esId).exec();\n\n        Map<String, ContainerNetwork> networks = info.getNetworkSettings().getNetworks();\n        if (networks == null) {\n            throw new IllegalStateException(\"Elasticsearch container has no network configuration\");\n        }\n\n        // Try to find the network endpoint - Docker may key by network name or ID\n        ContainerNetwork endpoint = findNetworkEndpoint(networks, network);\n        if (endpoint == null) {\n            throw new IllegalStateException(\n                \"Elasticsearch container is not connected to the expected network. \" +\n                \"Ensure both containers use the same Network instance.\"\n            );\n        }\n\n        // Prefer user-defined network aliases (skip Testcontainers auto-generated tc-* aliases)\n        if (endpoint.getAliases() != null && !endpoint.getAliases().isEmpty()) {\n            for (String alias : endpoint.getAliases()) {\n                if (StringUtils.isNotBlank(alias)) {\n                    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        }","sourceCodeStart":413,"sourceCodeEnd":449,"githubUrl":"https://github.com/testcontainers/testcontainers-java/blob/8e549514e3f01c57d70546fbb8599d138f3903e5/modules/elasticsearch/src/main/java/org/testcontainers/elasticsearch/KibanaContainer.java#L413-L449","documentation":"Thrown by resolveExistingEsDnsNameOnNetwork when Docker reports the Elasticsearch container's network endpoints but none matches the expected Network — findNetworkEndpoint returns null. The Elasticsearch container is not attached to the same network Kibana expects, so no DNS name on that network exists.","triggerScenarios":"Managed mode where the ES container was connected to a different Network instance than Kibana despite earlier checks (e.g. network set after construction, or a Docker network alias/ID mismatch between keying by name vs ID).","commonSituations":"Attaching networks programmatically after containers started; creating the Network once per test class on one container but a new instance on the other; Docker inspect keyed by network ID vs name edge cases.","solutions":["Ensure the exact same Network instance is passed to both elasticsearch.withNetwork() and kibana.withNetwork() before start().","Check docker network inspect <net> to confirm the ES container is attached.","Remove explicit networks so KibanaContainer creates and attaches both containers to one network automatically."],"exampleFix":"// before\nelasticsearch.withNetwork(Network.newNetwork());\nkibana.withNetwork(sharedNetwork); // ES not on this network\n// after\nelasticsearch.withNetwork(sharedNetwork);\nkibana.withNetwork(sharedNetwork);","handlingStrategy":"validation","validationCode":"Network shared = Network.newNetwork();\nelasticsearch.withNetwork(shared);\nkibana.withNetwork(shared);\nassert elasticsearch.getNetwork() == kibana.getNetwork();","typeGuard":"boolean esOnNetwork(ElasticsearchContainer es, Network n) { return n != null && n == es.getNetwork(); }","tryCatchPattern":"try { kibana.start(); } catch (IllegalStateException e) { if (e.getMessage().contains(\"not connected to the expected network\")) throw new ConfigException(\"Attach ES to the exact same Network instance as Kibana\"); throw e; }","preventionTips":["Pass the identical Network object to both containers before start()","Verify with docker network inspect that both containers are attached","Do not attach/modify networks after containers have started"],"tags":["network","docker","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"}