On Docker Desktop for Mac, host.docker.internal just works, and on a Linux server it just doesn’t. That catches people out when a container needs to talk to something running on the host itself, like a service installed outside Docker. You don’t have to look up the bridge IP and hard-code it, though. Docker has a special value for exactly this.
The value is host-gateway. Docker replaces it with the internal IP address of the host, and you give it a name through extra_hosts in Compose (or --add-host with docker run). The convention is to call it host.docker.internal, so the same config works on a Mac and on Linux:
services:
app:
image: example/app
extra_hosts:
- "host.docker.internal=host-gateway"
Inside the container, /etc/hosts now has a line for that name, and http://host.docker.internal:8000 reaches port 8000 on the host. The Docker docs show the same thing as a one-liner, which is a quick way to test it:
docker run --rm --add-host host.docker.internal=host-gateway \
curlimages/curl -s host.docker.internal:8000/hello
On Unraid, where you don’t write a Compose file for most containers, the --add-host part goes into the Extra Parameters field of the template. The = separator in Compose needs Compose v2.24.1 or newer; on older versions write host.docker.internal:host-gateway, which still works.
My caveat: the name only gets you to the host, not past its own rules. A service on the host that listens on 127.0.0.1 only won’t answer, because the container comes in through the Docker bridge, not through loopback. It has to listen on an address the bridge can reach, and a host firewall can still drop the traffic. I’d check both before blaming Docker.
Further reading: docker run reference: –add-host and host-gateway, Compose specification: extra_hosts