If youāre running a local development server and need to access it from within a Docker container, you can use host.docker.internal a Docker Desktop feature.
For example, if your local service is running on port 3000, you can access it inside the container using:
http://host.docker.internal:3000
This is especially useful when working with microservices, API calls, or testing Front-end applications inside a containerized environment.
Or as @meyay pointed out, install Docker Desktop on Linux
It works by default on macOS and Windows because you can run Linux containers on those operating systems only in a Linux virtual machine manually or using Docker Desktop, which runs a VM while you can still run the client on the host operating system. It should work the same way on Linux too when using Docker Desktop.
If you just use --add-host with the host-gateway alias, it will just point to the host gateway while Docker Desktopās host.docker.internal domain points to the ālocalhostā of your host machine so you donāt have to publish the ports on the LAN IP or any Docker network gateway IP.
If you want to access the local port without Docker Desktop, you can do it as I described here
Youāre absolutely right! host.docker.internal is a documented feature of Docker Desktop, regardless of the OS. Iāll update the post to make that clearer. Thanks for pointing it out!
Thatās a great point! Installing Docker Desktop on Linux enables host.docker.internal to work out of the box, just like on macOS and Windows. Thanks for the clarification!
Docker Desktopās host.docker.internal maps directly to the localhost of your host machine, making it more convenient than just using --add-host=host-gateway, which only points to the host gateway.
For those not using Docker Desktop, your alternative method for accessing local ports without it is definitely usefulā good explanation!