Docker Desktop installation on WIndows causes InetAddress.getLocalhost().getCanonicalHostname() to return host.docker.internal

Viewed 152

I'm using a Windows machine with Docker Desktop installed. My project does not live inside a docker container.

But as evident to https://youtrack.jetbrains.com/issue/IDEA-263484, it seems that Docker Desktop has overridden my host file to function properly, and the following code now returns an incorrect hostname.

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

This is my host file

#...
# 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

Is there another standard method (that is cross-platform) to detect real machine hostname that works with Docker Desktop installations?


As mentioned in the linked issue and in Rob's answer, I could probably add an entry in my host file to mitigate this issue, but this might not work too well if the IP gets changed, and requires additional setup.

1 Answers

It looks like getCanonicalHostname() takes the first entry for your IP address. It's not the best solution, but if you add an entry for 192.168.128.75 above the one for host.docker.internal, that one gets picked up.

Related