How to Interact with Ollama on your Host Machine while inside a Docker Container

If you’ve tried connecting a Docker container to Ollama running on your host machine, you’ve likely seen connection errors — even when using the correct IP address.
That happens because of how Ollama is configured by default.

This guide is for linux users only.
Before continuing, make sure you know How to Connect to Localhost from a Docker Container on Linux


The Problem

By default, Ollama only listens on 127.0.0.1, which means it only accepts connections that come directly from the host machine itself.
Any external connection — including those from Docker containers — will fail.

So even if your container tries to connect like this:

curl http://172.17.0.1:11434

the request will be rejected because Ollama is restricted to localhost.


The Solution

To make Ollama accessible to Docker (and other devices on your network), you must configure it to listen on all interfaces instead of just localhost.

That’s done by changing its host binding from 127.0.0.1 to 0.0.0.0.


Step-by-Step: Allow Docker to Connect to Ollama

1. Open the Ollama service configuration:

sudo systemctl edit ollama.service

2. Add the following lines:

[Service]
Environment="OLLAMA_HOST=0.0.0.0:11434"

This instructs Ollama to accept connections from any network interface, including Docker containers.

3. Reload and restart Ollama:

sudo systemctl daemon-reload 
sudo systemctl restart ollama

4. Verify that it’s listening on all interfaces:

ss -lntp | grep 11434

The output should be similar to: *:11434


Quick Summary

  • Ollama only listens on 127.0.0.1 by default
  • That blocks external and Docker connections
  • To fix it, edit the service: Environment="OLLAMA_HOST=0.0.0.0:11434"
  • Restart Ollama and connect using your host IP (172.17.0.1)

With this setup, your Docker containers can now interact with Ollama without any connection issues.

References

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