Monday, May 26, 2025

How to access Localhost from within a container?

To refer to the host machine from within a Docker container, the correct hostname is:

host.docker.internal

This works in:

Docker Desktop for Mac and Windows

Docker with WSL2 on Windows

For Linux:

On Linux, host.docker.internal is not available by default. You have to use alternative methods like:

Option 1: Use the host's IP address

You can find the host IP from inside the container using:

ip route | awk '/default/ { print $3 }'

This gives the host IP on the default network bridge.

Option 2: Run Docker with host network (not for Windows/macOS)

If your container needs to access localhost services and you don’t mind sharing the host network:

docker run --network host your_image

--network host works only on Linux.

To summarise, host.docker.internal works on windows 

Use --network host or get host IP manually works on Linux 



No comments:

Post a Comment