Localhost not reachable on windows 10 wsl2

Starting from monday, everytime i need to call my localhost Chrome say that is not available even if with ping i'm able to see it. I already try to:

  • delete Chrome dns cache
  • delete firefix dns cache
  • delete windows cache

After some research online and tests, i saw that with ipconfig /flushdns and a restart of the computer, start working again.

The problem is that everyday the problem persists and my routine now is:

  • turn on the PC
  • execute ipconfig /flushdns via windows terminal
  • restart the PC
  • run the application via WLS2

This is not sustainable, there is a way to make it working permanent?

Here is my hosts file:

# localhost name resolution is handled within DNS itself.
127.0.0.1       localhost
::1             localhost

# Added by Docker Desktop
192.168.X.X host.docker.internal 
192.168.X.X 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

EDIT: The application is running in ubuntu subsystem on wsl2, the browsers are running on windows.

EDIT: Here is what the application prints (it's a django application):

Performing system checks...

System check identified no issues (5 silenced).
April 14, 2021 - 07:00:43
Django version 2.2.16, using settings 'geonode.local_settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

And this is the chrome screenshot:

enter image description here

Translated:

Unable to reach the website. 
Connection refused for localhost.
Try to:
- verify connection
- check proxy and firewall
ERR_CONNECTION_REFUSED

Urls tested:

http://127.0.0.1:8000/
http://localhost:8000/

Solution 1:

My guess is what you are running into, based on how you've described it, is something (possibly kubernetes) is overwriting the 127.0.0.1 entry in your windows hostfile [consider this foundationally different from the ubuntu hosts file in the windows linux subsystem] and then when the ubuntu server is launched, by the windows linux subsystem, it creates its own hosts entry from that file, and creates its own loopback hostname that is incorrect.

Some point later, the windows hostsfile localhost entry is overwritten as myth2.localdomain, and when you attempt to access localhost in your browser it performs a lookup for myth2.localdomain and the request is sent to the ubuntu host, listening only on localhost for myth.localdomain and the request fails.

you flush dns and restart, both systems have the same hosts file, the request work for a time.

you can avoid this in 2 ways.

first have your ubuntu app listen on 0.0.0.0:8000 (bind to all addresses, not just localhost)

second get the ubuntu system ip address or hostname, and make a host file entry on the windows machine with that address and ubuntuapp.whatever.com and use that URL in your browser

But you're right it's unsustainable, and not becuase of the windows linux subsystem, it's because your app is listening on a specific address, that is abstracted one layer from your windows machine, and that address pair is being overwritten by something else.