{"record":{"id":"1f0b305e1a80cc18","repo":"baomidou/mybatis-plus","slug":"unable-to-retrieve-localhost","errorCode":null,"errorMessage":"Unable to retrieve localhost","messagePattern":"Unable to retrieve localhost","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/toolkit/NetUtils.java","lineNumber":78,"sourceCode":"                            if (address instanceof Inet4Address\n                                && !address.isLoopbackAddress()\n                                && isPreferredAddress(address)) {\n                                result = address;\n                            }\n                        }\n                    }\n                }\n            }\n        } catch (IOException exception) {\n            LOG.error(\"Cannot get first non-loopback address\", exception);\n        }\n        if (result != null) {\n            return result;\n        }\n        try {\n            return InetAddress.getLocalHost();\n        } catch (UnknownHostException e) {\n            throw new RuntimeException(\"Unable to retrieve localhost\");\n        }\n    }\n\n    boolean isPreferredAddress(InetAddress address) {\n        final List<String> preferredNetworks = this.netProperties.getPreferredNetworks();\n        if (preferredNetworks.isEmpty()) {\n            return true;\n        }\n        for (String regex : preferredNetworks) {\n            final String hostAddress = address.getHostAddress();\n            if (hostAddress.matches(regex) || hostAddress.startsWith(regex)) {\n                return true;\n            }\n        }\n        return false;\n    }\n\n    boolean ignoreInterface(String interfaceName) {","sourceCodeStart":60,"sourceCodeEnd":96,"githubUrl":"https://github.com/baomidou/mybatis-plus/blob/bf67d907478c724120bf76292da54abf9e73c2b3/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/toolkit/NetUtils.java#L60-L96","documentation":"NetUtils tries to find the machine's non-loopback address (for identifier/trace purposes) and, failing that, falls back to InetAddress.getLocalHost(). If the OS cannot resolve a localhost entry (no /etc/hosts mapping, DNS misconfiguration), the UnknownHostException is rethrown as RuntimeException('Unable to retrieve localhost'). It is an environment problem, not a logic problem.","triggerScenarios":"Running on a host where getLocalHost() throws UnknownHostException: hostname not mapped in /etc/hosts (common in minimal Docker/alpine images or Kubernetes pods with randomized hostnames), or a security manager/JVM security policy blocks InetAddress.getLocalHost().","commonSituations":"Docker containers with hostnames absent from /etc/hosts; hardened containers where networking is restricted; CI runners with unusual network setups; k8s pods with generated hostnames.","solutions":["In Docker/k8s, ensure the container hostname resolves: add it to /etc/hosts (docker run --add-host, or an initContainer/entrypoint appending '127.0.0.1 $HOSTNAME').","On VMs/bare metal, add '<ip> <hostname>' to /etc/hosts so getLocalHost() resolves.","Check any JVM security policy / SecurityManager that might block local address resolution and grant it or remove it.","If neither is possible, avoid the code path that triggers NetUtils (identifier generation needing a local address) and supply the identifier explicitly."],"exampleFix":"# before: container hostname unresolvable -> RuntimeException on getLocalHost()\ndocker run myapp:latest\n\n# after: make the hostname resolvable\ndocker run --add-host=\"$(hostname):127.0.0.1\" myapp:latest\n# or in the entrypoint:\necho \"127.0.0.1 $(hostname)\" >> /etc/hosts","handlingStrategy":"try-catch","validationCode":"boolean localResolvable;\ntry {\n    InetAddress.getLocalHost();\n    localResolvable = true;\n} catch (UnknownHostException e) {\n    localResolvable = false;\n}\nif (!localResolvable) { /* fix /etc/hosts or container hostname before starting app */ }","typeGuard":null,"tryCatchPattern":"try {\n    String addr = NetUtils.getLocalHost();\n} catch (RuntimeException e) {\n    // fall back to a configured identifier\n    log.warn(\"local address unresolvable; using configured node id\", e);\n}","preventionTips":["Bake '127.0.0.1 $(hostname)' into container images/entrypoints.","Run a startup check that resolves InetAddress.getLocalHost() and fails fast with an actionable message."],"tags":["network","environment","docker","hostname"],"backgroundTag":null,"analyzedSha":"bf67d907478c724120bf76292da54abf9e73c2b3","analyzedAt":"2026-08-14T15:17:09.543Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}