在WIndows上安装Docker Desktop会导致InetAddress.getLocalhost().getCa



我使用的是安装了Docker Desktop的Windows机器。我的项目不是生活在docker容器中。

但显而易见https://youtrack.jetbrains.com/issue/IDEA-263484,Docker Desktop似乎已经覆盖了我的主机文件以正常工作,下面的代码现在返回了一个不正确的主机名。

InetAddress.getLocalhost().getCanonicalHostname() // host.docker.internal

这是我的主机文件

#...
# localhost name resolution is handled within DNS itself.
#   127.0.0.1       localhost
#   ::1             localhost
# Added by Docker Desktop
192.168.128.75 host.docker.internal
192.168.128.75 gateway.docker.internal
# To allow the same kube context to work on the host and the container:
127.0.0.1 kubernetes.docker.internal
# End of section

是否有另一种标准方法(即跨平台(来检测适用于Docker Desktop安装的真实机器主机名?


正如链接问题和Rob的回答中所提到的,我可能会在主机文件中添加一个条目来缓解这个问题,但如果IP发生更改,并且需要额外的设置,这可能不会很好地工作。

看起来getCanonicalHostname()为您的IP地址获取第一个条目。这不是最好的解决方案,但如果在host.docker.internal的条目之上添加192.168.128.75的条目,则该条目会被选中。

最新更新