# Can't access localhost API from within docker container using host.docker.internal

**URL:** <https://forums.docker.com/t/cant-access-localhost-api-from-within-docker-container-using-host-docker-internal/150788>\
**Category:** General\
**Tags:** docker, docker-compose, wsl2, windows\
**Created:** [January 8, 2026, 2:35pm UTC](https://forums.docker.com/t/cant-access-localhost-api-from-within-docker-container-using-host-docker-internal/150788 "2026-01-08T14:35:37Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![aliahmad05](https://sea3.discourse-cdn.com/docker/user_avatar/forums.docker.com/aliahmad05/32/54541_2.png) [@aliahmad05](https://forums.docker.com/u/aliahmad05)\
**Post date:** [January 8, 2026, 2:35pm UTC](https://forums.docker.com/t/cant-access-localhost-api-from-within-docker-container-using-host-docker-internal/150788/1 "2026-01-08T14:35:37Z")

</div>

Hi I am running docker from within WSL2, docker dekstop version 4.55.0.

I have a simple Spring boot API that is exposing prometheus endpoint. I can’t seem to curl it from within my docker container using host.docker.internal.

```auto
❯ docker run --rm -it --add-host=host.docker.internal:host-gateway curlimages/curl sh
~ $ curl http://host.docker.internal:8081/actuator/prometheus
curl: (7) Failed to connect to host.docker.internal port 8081 after 3 ms: Could not connect to server

```

However, when I ran the command

```auto
❯ ifconfig | grep -E "([0-9]{1,3}\.){3}[0-9]{1,3}" | grep -v 127.0.0.1 | awk '{ print $2 }' | cut -f2 -d: | head -n1
172.31.230.230

```

I was able to curl it using this IP address.

```auto
❯ docker run --rm -it --add-host=host.docker.internal:host-gateway curlimages/curl sh
~ $ curl -v http://172.31.230.230:8081/actuator/prometheus
* Trying 172.31.230.230:8081...
* Established connection to 172.31.230.230 (172.31.230.230 port 8081) from 172.17.0.2 port 34284
* using HTTP/1.x
> GET /actuator/prometheus HTTP/1.1
> Host: 172.31.230.230:8081
> User-Agent: curl/8.18.0
> Accept: */*
>
* Request completely sent off
< HTTP/1.1 200
< Content-Type: text/plain;version=0.0.4;charset=utf-8
< Content-Length: 24137
< Date: Thu, 08 Jan 2026 14:21:30 GMT
<
# HELP application_ready_time_seconds Time taken for the application to be ready to service requests
# TYPE application_ready_time_seconds gauge
application_ready_time_seconds{main_application_class="com.ecommerce.product.ProductApplication"} 14.495

```

Is there any reason as to why host.docker.internal wasn’t working?  
The reason I ask is that I am running prometheus inside docker and was trying to scrape from `host.docker.internal:8081` and it wasn’t working. After changing it to that IP address it worked. Why is that the case? Why didn’t host.docker.internal work.

---

<div class="post-metadata">

**Author:** ![bluepuma77](https://sea3.discourse-cdn.com/docker/user_avatar/forums.docker.com/bluepuma77/32/32688_2.png) [@bluepuma77](https://forums.docker.com/u/bluepuma77)\
**Post date:** [January 8, 2026, 4:09pm UTC](https://forums.docker.com/t/cant-access-localhost-api-from-within-docker-container-using-host-docker-internal/150788/2 "2026-01-08T16:09:44Z")

</div>

Docker was developed as a Linux tool. Docker Desktop on Windows is usually running all containers inside a Linux VM. As far as I know `host.docker.internal` is provided by Docker Desktop automatically ([doc](https://docs.docker.com/desktop/features/networking/networking-how-tos/)):

> `host.docker.internal` Resolves to the internal IP address of your host

Using `--add-host=host.docker.internal:host-gateway` is usually a workaround for Docker-CE on Linux, when not using Docker Desktop.

---

<div class="post-metadata">

**Author:** ![aliahmad05](https://sea3.discourse-cdn.com/docker/user_avatar/forums.docker.com/aliahmad05/32/54541_2.png) [@aliahmad05](https://forums.docker.com/u/aliahmad05)\
**Post date:** [January 8, 2026, 4:13pm UTC](https://forums.docker.com/t/cant-access-localhost-api-from-within-docker-container-using-host-docker-internal/150788/3 "2026-01-08T16:13:52Z")

</div>

Running the container without `--add-host=host.docker.internal:host-gateway` on WSL I can still curl the API using `curl -v http://172.31.230.230:8081/actuator/prometheus`, but when I use `host.docker.internal` I get

```auto
~ $ curl -v http://host.docker.internal:8081/actuator/prometheus
* Host host.docker.internal:8081 was resolved.
* IPv6: (none)
* IPv4: 192.168.65.254
* Trying 192.168.65.254:8081...
* connect to 192.168.65.254 port 8081 from 172.17.0.2 port 58662 failed: Connection refused
* Failed to connect to host.docker.internal port 8081 after 4 ms: Could not connect to server
* closing connection #0
curl: (7) Failed to connect to host.docker.internal port 8081 after 4 ms: Could not connect to server

```

---

<div class="post-metadata">

**Author:** ![rimelek](https://sea3.discourse-cdn.com/docker/user_avatar/forums.docker.com/rimelek/32/41602_2.png) [@rimelek](https://forums.docker.com/u/rimelek)\
**Post date:** [January 8, 2026, 6:52pm UTC](https://forums.docker.com/t/cant-access-localhost-api-from-within-docker-container-using-host-docker-internal/150788/4 "2026-01-08T18:52:49Z")

</div>

`host.docker.internal` in Docker Desktop points to the localhost of the actual host. The IP you shared looks like the IP of WSL. If the service on port 8081 isn’t listening on localhost, only on the WSL IP, `host.docker.internal` will not help.

I rarely use WSL for this, so I am not sure about when and how ports are forwarded from Windows to WSL, but I think the service on port 8081 has to be available on the localhost of Windows.

Cany ou try

```auto
curl http://localhost:8081/actuator/prometheus

```

on Windows in Powershell? Or just try `http://localhost:8081/actuator/prometheus` in a web browser on Windows.

---

<div class="post-metadata">

**Author:** ![aliahmad05](https://sea3.discourse-cdn.com/docker/user_avatar/forums.docker.com/aliahmad05/32/54541_2.png) [@aliahmad05](https://forums.docker.com/u/aliahmad05)\
**Post date:** [January 8, 2026, 6:55pm UTC](https://forums.docker.com/t/cant-access-localhost-api-from-within-docker-container-using-host-docker-internal/150788/5 "2026-01-08T18:55:33Z")

</div>

I can access the `http://localhost:8081/actuator/prometheus` in a web browser and get this from powershell:

```auto
StatusCode : 200
StatusDescription :
Content : # HELP application_ready_time_seconds Time taken for the application
                    to be ready to service requests
                    # TYPE application_ready_time_seconds gauge
                    application_ready_time_seconds{main_application_class="...
RawContent : HTTP/1.1 200
                    Keep-Alive: timeout=60
                    Connection: keep-alive
                    Content-Length: 23600
                    Content-Type: text/plain;version=0.0.4;charset=utf-8
                    Date: Thu, 08 Jan 2026 18:54:07 GMT

                    # HELP application_re...
Forms : {}
Headers : {[Keep-Alive, timeout=60], [Connection, keep-alive], [Content-Length,
                    23600], [Content-Type, text/plain;version=0.0.4;charset=utf-8]...}
Images : {}
InputFields : {}
Links : {}
ParsedHtml : mshtml.HTMLDocumentClass
RawContentLength : 23600

```

---

<div class="post-metadata">

**Author:** ![meyay](https://sea3.discourse-cdn.com/docker/user_avatar/forums.docker.com/meyay/32/34000_2.png) [@meyay](https://forums.docker.com/u/meyay)\
**Post date:** [January 8, 2026, 7:11pm UTC](https://forums.docker.com/t/cant-access-localhost-api-from-within-docker-container-using-host-docker-internal/150788/6 "2026-01-08T19:11:21Z")

</div>

Please share the output of `netstat -p tcp -q | Select-String -Pattern ":8081"`, so we can see that the process actually binds `0.0.0.0:8081`, and not just `127.0.0.1:8081`.

---

<div class="post-metadata">

**Author:** ![aliahmad05](https://sea3.discourse-cdn.com/docker/user_avatar/forums.docker.com/aliahmad05/32/54541_2.png) [@aliahmad05](https://forums.docker.com/u/aliahmad05)\
**Post date:** [January 8, 2026, 7:14pm UTC](https://forums.docker.com/t/cant-access-localhost-api-from-within-docker-container-using-host-docker-internal/150788/7 "2026-01-08T19:14:51Z")

</div>

This is the output I get from running it in powershell:

```auto
>netstat -p tcp -q | Select-String -Pattern ":8081"

  TCP 172.31.224.1:52946 172.31.230.230:8081 ESTABLISHED

```

---

<div class="post-metadata">

**Author:** ![meyay](https://sea3.discourse-cdn.com/docker/user_avatar/forums.docker.com/meyay/32/34000_2.png) [@meyay](https://forums.docker.com/u/meyay)\
**Post date:** [January 8, 2026, 7:56pm UTC](https://forums.docker.com/t/cant-access-localhost-api-from-within-docker-container-using-host-docker-internal/150788/8 "2026-01-08T19:56:34Z")

</div>

I am surprised by the output. It only shows an active connection, but doesn’t show that port :8081 listens at all…

---

<div class="post-metadata">

**Author:** ![meyay](https://sea3.discourse-cdn.com/docker/user_avatar/forums.docker.com/meyay/32/34000_2.png) [@meyay](https://forums.docker.com/u/meyay)\
**Post date:** [January 8, 2026, 8:31pm UTC](https://forums.docker.com/t/cant-access-localhost-api-from-within-docker-container-using-host-docker-internal/150788/9 "2026-01-08T20:31:18Z")

</div>

After discussing with @rimelek seems I was mistaken about how `host.docker.internal` works, and that it should work, even if the process only binds on 127.0.0.1:8081 on the host.

---

<div class="post-metadata">

**Author:** ![rimelek](https://sea3.discourse-cdn.com/docker/user_avatar/forums.docker.com/rimelek/32/41602_2.png) [@rimelek](https://forums.docker.com/u/rimelek)\
**Post date:** [January 8, 2026, 8:33pm UTC](https://forums.docker.com/t/cant-access-localhost-api-from-within-docker-container-using-host-docker-internal/150788/10 "2026-01-08T20:33:53Z")

</div>

I cannot reproduce this issue. I can make `host.docker.internal` not work, but than the URL doesn’T work from the web browser either.

It could still help if you could tell us more about how you start that Sprint boot API with the prometheus endpoint. Is it running in a container to which you forward ports? Is it running directly on Windows or inside a WSL distribution? Do you know what IP addresses that is supposed to listen on? Can you find any configuration related to that if you didn’t set it?

---

<div class="post-metadata">

**Author:** ![aliahmad05](https://sea3.discourse-cdn.com/docker/user_avatar/forums.docker.com/aliahmad05/32/54541_2.png) [@aliahmad05](https://forums.docker.com/u/aliahmad05)\
**Post date:** [January 8, 2026, 8:35pm UTC](https://forums.docker.com/t/cant-access-localhost-api-from-within-docker-container-using-host-docker-internal/150788/11 "2026-01-08T20:35:21Z")

</div>

The Spring Boot API is running inside WSL, it’s not running inside any container.I am not sure about the last point I’ve set it to run on port 8081.

---

<div class="post-metadata">

**Author:** ![rimelek](https://sea3.discourse-cdn.com/docker/user_avatar/forums.docker.com/rimelek/32/41602_2.png) [@rimelek](https://forums.docker.com/u/rimelek)\
**Post date:** [January 8, 2026, 8:40pm UTC](https://forums.docker.com/t/cant-access-localhost-api-from-within-docker-container-using-host-docker-internal/150788/12 "2026-01-08T20:40:47Z")

</div>

> [@aliahmad05](#):
>
> I am not sure about the last point I’ve set it to run on port 8081.

Yes, but on what IP? I gues if you don’t know, it is on default setting. I don’t know what the default is, just assume it should be on all avalable IP addresses.

I am trying to figure out how it is possible that the endpoint works for you from a webbrowser on “localhost” but not when you are using `host.docker.internal` without `--add-host`.

I know you are using Docker Desktop, but have you also installed Docker inside the WSL distribution? Docker Desktop should override it, but I have no other ideas what could break `host.docker.internal`

---

<div class="post-metadata">

**Author:** ![aliahmad05](https://sea3.discourse-cdn.com/docker/user_avatar/forums.docker.com/aliahmad05/32/54541_2.png) [@aliahmad05](https://forums.docker.com/u/aliahmad05)\
**Post date:** [January 8, 2026, 8:44pm UTC](https://forums.docker.com/t/cant-access-localhost-api-from-within-docker-container-using-host-docker-internal/150788/13 "2026-01-08T20:44:34Z")

</div>

I think it’s on default setting. I think I might have installed docker inside the WSL distribution. Running the command `docker context ls` gives the following output:

```auto
NAME DESCRIPTION DOCKER ENDPOINT ERROR
default * Current DOCKER_HOST based configuration unix:///var/run/docker.sock      
desktop-linux Docker Desktop npipe:////./pipe/dockerDesktopLinuxEngine

```

and also:

```auto
❯ which docker
/usr/bin/docker

```

Could this cause the issue?

---

<div class="post-metadata">

**Author:** ![rimelek](https://sea3.discourse-cdn.com/docker/user_avatar/forums.docker.com/rimelek/32/41602_2.png) [@rimelek](https://forums.docker.com/u/rimelek)\
**Post date:** [January 8, 2026, 8:49pm UTC](https://forums.docker.com/t/cant-access-localhost-api-from-within-docker-container-using-host-docker-internal/150788/14 "2026-01-08T20:49:09Z")

</div>

That is just the client. The question is whether you installed the daemon or not. And if that is running. Running the following command should show if a Docker CE daemon is running in WSL:

```auto
sudo systemctl status docker

```

---

<div class="post-metadata">

**Author:** ![aliahmad05](https://sea3.discourse-cdn.com/docker/user_avatar/forums.docker.com/aliahmad05/32/54541_2.png) [@aliahmad05](https://forums.docker.com/u/aliahmad05)\
**Post date:** [January 8, 2026, 8:49pm UTC](https://forums.docker.com/t/cant-access-localhost-api-from-within-docker-container-using-host-docker-internal/150788/15 "2026-01-08T20:49:34Z")

</div>

```auto
❯ sudo systemctl status docker
Unit docker.service could not be found.

```

---

<div class="post-metadata">

**Author:** ![rimelek](https://sea3.discourse-cdn.com/docker/user_avatar/forums.docker.com/rimelek/32/41602_2.png) [@rimelek](https://forums.docker.com/u/rimelek)\
**Post date:** [January 8, 2026, 8:55pm UTC](https://forums.docker.com/t/cant-access-localhost-api-from-within-docker-container-using-host-docker-internal/150788/16 "2026-01-08T20:55:01Z")

</div>

So no daemon. Do you have anything on Windows with which you can run a service on any port? I used python3 on Windows like this:

```auto
python3 -m http.server --bind 127.0.0.1 8082

```

and in an other terminal on Windows

```auto
docker run --rm -it curlimages/curl http://host.docker.internal:8082

```

Just to see what works for you and what not.

---

<div class="post-metadata">

**Author:** ![aliahmad05](https://sea3.discourse-cdn.com/docker/user_avatar/forums.docker.com/aliahmad05/32/54541_2.png) [@aliahmad05](https://forums.docker.com/u/aliahmad05)\
**Post date:** [January 8, 2026, 8:58pm UTC](https://forums.docker.com/t/cant-access-localhost-api-from-within-docker-container-using-host-docker-internal/150788/17 "2026-01-08T20:58:13Z")

</div>

```auto
❯ python3 -m http.server --bind 127.0.0.1 8087
Serving HTTP on 127.0.0.1 port 8087 (http://127.0.0.1:8087/) ...
127.0.0.1 - - [08/Jan/2026 20:55:47] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [08/Jan/2026 20:56:05] "GET / HTTP/1.1" 200 -

```

```auto
❯ docker run --rm -it curlimages/curl http://host.docker.internal:8087
<!DOCTYPE HTML>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Directory listing for /</title>
</head>
<body>
<h1>Directory listing for /</h1>
<hr>
<ul>
<li><a href=".data/">.data/</a></li>
<li><a href="docker-compose.yaml">docker-compose.yaml</a></li>
<li><a href="grafana/">grafana/</a></li>
<li><a href="init.sql">init.sql</a></li>
<li><a href="logging/">logging/</a></li>
<li><a href="prometheus/">prometheus/</a></li>
</ul>
<hr>
</body>
</html

```

Strange it worked for this. It could be a spring boot related issue that isn’t mapping it properly.

---

<div class="post-metadata">

**Author:** ![rimelek](https://sea3.discourse-cdn.com/docker/user_avatar/forums.docker.com/rimelek/32/41602_2.png) [@rimelek](https://forums.docker.com/u/rimelek)\
**Post date:** [January 8, 2026, 9:10pm UTC](https://forums.docker.com/t/cant-access-localhost-api-from-within-docker-container-using-host-docker-internal/150788/18 "2026-01-08T21:10:37Z")

</div>

Can you try the same, but now running the python http server inside the WSL distribution the same way you did on Windows?

---

<div class="post-metadata">

**Author:** ![aliahmad05](https://sea3.discourse-cdn.com/docker/user_avatar/forums.docker.com/aliahmad05/32/54541_2.png) [@aliahmad05](https://forums.docker.com/u/aliahmad05)\
**Post date:** [January 8, 2026, 9:11pm UTC](https://forums.docker.com/t/cant-access-localhost-api-from-within-docker-container-using-host-docker-internal/150788/19 "2026-01-08T21:11:20Z")

</div>

The output above was the output from running it inside WSL

---

<div class="post-metadata">

**Author:** ![rimelek](https://sea3.discourse-cdn.com/docker/user_avatar/forums.docker.com/rimelek/32/41602_2.png) [@rimelek](https://forums.docker.com/u/rimelek)\
**Post date:** [January 8, 2026, 9:12pm UTC](https://forums.docker.com/t/cant-access-localhost-api-from-within-docker-container-using-host-docker-internal/150788/20 "2026-01-08T21:12:08Z")

</div>

Then please, try running the python http server on Windows. That was my original recommendation

EDIT: On second thought, it might not be necessary, as your spring boot is also running in WSL.

[Next page](https://forums.docker.com/t/cant-access-localhost-api-from-within-docker-container-using-host-docker-internal/150788.md?page=2)
