A Docker container running on my Windows computer cannot call REST services at http://localhost:8380/api/

This is in a dev. environment on my Windows 11 computer. I’m trying to call from within the container REST services that run on my computer that hosts docker run... Based on information I’d found on the web, I’d tried replacing “localhost” with “host.docker.internal” and now I’m getting System.Net.WebException: The remote name could not be resolved: 'host.docker.internal' . Could anyone please enlighten me on how should I proceed? docker run --network="host" does not work either, which is expected on Windows.

docker version
Client:
 Version:           27.0.3
 API version:       1.46
 Go version:        go1.21.11
 Git commit:        7d4bcd8
 Built:             Sat Jun 29 00:03:32 2024
 OS/Arch:           windows/amd64
 Context:           desktop-windows

Server: Docker Desktop 4.32.0 (157355)
 Engine:
  Version:          27.0.3
  API version:      1.46 (minimum version 1.24)
  Go version:       go1.21.11
  Git commit:       662f78c
  Built:            Sat Jun 29 00:02:13 2024
  OS/Arch:          windows/amd64
  Experimental:     false

I’m not sure that host.docker.internal would work in a Windows container. That was required because Linux containers run inside a virtual machine and simply using the docker bridge gateway would not work. There is no host.docker.internal in Docker CE, but there is a special keyword called `host-gateway which you can use to set any domain to point to your shot machine’s IP addres on the Docker bridge. Maybe it can help on Windows as ell. Here is where I wrote about it:

According to 4715 likes, the host.docker.internal should work in the Windows world: nginx - From inside of a Docker container, how do I connect to the localhost of the machine? - Stack Overflow I seem to remember that in the past I was able to use even http://localhost/

OK, things may have changed in the meantime. The question is how should I proceed now while running in a Windows container to call REST services running on the local host computer?

It seems that post is about Linux containers on Windows and macOS, but I tried this command from the MS docs

docker run -it mcr.microsoft.com/windows/nanoserver:ltsc2022 cmd.exe

And I ran this on Windows with my installed Python interpreter:

python3.exe -m http.server 9000

After I allowed Python to listen on the port,

curl http://host.docker.internal:9000

worked in the container but only because the python server listened on all interfaces. Accessing the server created by this command on the host didn’t work:

python3.exe -m http.server --bind 127.0.0.1 9000

So the difference is that in a Windows container that domain is added to the hosts file of the container and the domain points to the IP address of the host, while in a Linux container in Docker Desktop, there is a DNS service that can resolve that domain and and the requests are proxied to the localhost of the host machine. So it will not work with Windows containers without that special proxy and if a library requires a DNS server to give you the IP of the host, domains added to the hosts file will not work either so domain resolution will fail.

If you mean from the container you could send a request to localhost, that would work only if you use the host network. Otherwise the containers localhost is just not the same as the host’s localhost.