How to Connect to Localhost from a Docker Container on Linux

If you’ve ever tried connecting a Docker container to an app running on your host machine in Linux, you’ve probably noticed something confusing — localhost inside the container doesn’t point to your host. Instead, it points to the container itself.

The Issue

Docker provides a convenient alias, host.docker.internal, for macOS and Windows users. It automatically maps back to the host system, allowing containers to communicate with services running outside Docker.

But on Linux, that hostname doesn’t exist by default. So when you try something like:

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

it simply fails.

The Solution

On Linux, Docker creates a virtual network bridge interface called docker0.
This bridge connects your containers to your host system and assigns your host a specific IP address within the Docker network.

To find that IP, run the following command on your host:

ip addr show docker0

You’ll see an output similar to this:

inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0

That IP (172.17.0.1), the one before brd, is your host machine’s network address as seen from Docker containers.

Now, from inside any container, you can connect to services running on your host like this:

curl http://172.17.0.1:11434

The example above will connect to Ollama running on your local machine at port 11434. This works for any other API on your linux machine as well.

Quick Summary

  • localhost inside Docker is not equal to your host machine
  • host.docker.internal works on macOS & Windows only
  • On Linux, find your host IP using: ip addr show docker0
  • Then use that IP (e.g., 172.17.0.1) to connect to your host machine.

And that’s it — your container can now talk to your Linux host without special Docker flags or network modes.

Even with the above solution, I still had trouble connecting with Ollama from the docker container. Read this article to see how I resolve it.

If this post helped you, consider sharing it — it really helps others discover useful resources. Thanks.