When calling a local Ollama instance from Python scripts, AI agents, or Docker containers, you may encounter:
httpx.ConnectError: [Errno 111] Connection refused
# or
urllib.error.URLError: <urlopen error [Errno 61] Connection refused>
# or
Failed to connect to localhost port 11434: Connection refused
This error happens for two primary reasons:
1. Host Binding: By default, Ollama binds exclusively to loopback 127.0.0.1, rejecting inbound requests originating outside localhost (such as from Docker containers or local network peers).
2. Daemon Status: The Ollama background service is not running or crashed.
Here is the exact step-by-step fix for Linux, macOS, and Docker environments.
Step 1: Ensure Ollama Service is Active
Before troubleshooting network bindings, check if the service is alive:
# On Linux (systemd)
systemctl status ollama
# If stopped, restart it:
sudo systemctl restart ollama
# On macOS
# Ensure the Ollama app icon is running in your menu bar, or start via terminal:
ollama serve
Test with curl http://127.0.0.1:11434/. If running, it will respond with:
Ollama is running
Step 2: Fix Host Binding (0.0.0.0) for Network & Docker Access
If calling Ollama from a Docker container or another machine, Ollama must listen on all network interfaces (0.0.0.0), not just 127.0.0.1.
On Linux (systemd service)
Edit the service configuration:
sudo systemctl edit ollama.service
Add the following lines under [Service]:
[Service]
Environment="OLLAMA_HOST=0.0.0.0:11434"
Environment="OLLAMA_ORIGINS=*"
Save, reload, and restart:
sudo systemctl daemon-reload
sudo systemctl restart ollama
On macOS
Quit the Ollama application, then launch it with the environment variable set:
launchctl setenv OLLAMA_HOST "0.0.0.0:11434"
# Or run directly in terminal:
OLLAMA_HOST=0.0.0.0:11434 ollama serve
Step 3: Accessing Ollama from Docker Containers
If your Python app runs inside Docker and Ollama runs on your host machine, localhost:11434 will point to the container itself, causing a connection failure.
Use host.docker.internal instead of localhost:
import os
from openai import OpenAI
# Automatically use host.docker.internal when inside Docker
base_url = os.getenv("OLLAMA_BASE_URL", "http://host.docker.internal:11434/v1")
client = OpenAI(
base_url=base_url,
api_key="ollama"
)
response = client.chat.completions.create(
model="deepseek-r1:14b",
messages=[{"role": "user", "content": "Hello!"}]
)
print(response.choices[0].message.content)
In docker-compose.yml, ensure the container can resolve the host:
services:
agent_app:
build: .
extra_hosts:
- "host.docker.internal:host-gateway"
environment:
- OLLAMA_BASE_URL=http://host.docker.internal:11434/v1
Next Steps
To implement autonomous tool calling with local models, follow our step-by-step tutorial on Local LLM Tool Calling with Ollama and Python.
To check memory requirements before downloading large models, use our Local LLM & DeepSeek-R1 VRAM Hardware Sizer.




Leave a Reply