Fix Connection Refused (Errno 111) When Using Ollama with AutoGPT in Docker
Original question: Connection Refused ([Errno 111]) When Using Ollama with AutoGPT
Replace localhost with host.docker.internal in the Ollama connection string inside your AutoGPT configuration. When both services run in Docker containers, localhost inside a container refers to the container itself, not the host machine. Use http://host.docker.internal:11434/ to reach Ollama running on the host from the AutoGPT container.
The Full Answer
The error [Errno 111] Connection refused when connecting Ollama to AutoGPT in Docker Compose has a specific cause: Docker container networking. This section explains the root problem and provides the exact fix, along with verification steps.
Why the Error Occurs
When you run Ollama and AutoGPT using Docker Compose, each service runs in its own container. Docker containers have isolated network namespaces. The address localhost (127.0.0.1) inside a container refers to the container's own loopback interface, not the host machine's. So when AutoGPT tries to reach Ollama at http://localhost:11434/, it attempts to connect to port 11434 inside the AutoGPT container, where no service is listening. This produces the [Errno 111] Connection refused error.
As confirmed in the accepted Stack Overflow answer by efremidze, the solution is to use a special DNS name that Docker provides to reach the host from within a container.
The Fix: Use host.docker.internal
Replace localhost with host.docker.internal in the Ollama endpoint URL. The correct URL is:
http://host.docker.internal:11434/
This works because host.docker.internal is a DNS name that Docker automatically resolves to the host machine's IP address from inside any container. It is available on Docker Desktop for Windows, macOS, and Linux (with some configuration).
Step-by-Step Implementation
-
Locate the configuration file in your AutoGPT project where the Ollama endpoint is defined. This is typically in a
.envfile or aconfig.pyfile. Look for a variable likeOLLAMA_BASE_URL,LLM_PROVIDER_URL, orOLLAMA_HOST. -
Change the value from:
OLLAMA_BASE_URL=http://localhost:11434/to:
OLLAMA_BASE_URL=http://host.docker.internal:11434/ -
Save the file and restart your Docker Compose services:
docker-compose down docker-compose up -d -
Verify that AutoGPT can now reach Ollama by checking the logs:
docker-compose logs autogptLook for a successful connection message or the absence of the
[Errno 111]error.
Verifying Ollama is Running on the Host
Before applying the fix, confirm that Ollama is actually running on the host machine. The original poster in the Stack Overflow question did this correctly:
$ curl http://localhost:11434/
Ollama is running
If you get a different response (e.g., curl: (7) Failed to connect to localhost port 11434: Connection refused), then Ollama is not running. Start it with:
ollama serve
Or ensure the Ollama service is enabled and started on your system.
Alternative: Use the Host's IP Address
If host.docker.internal does not work on your platform (e.g., older Linux Docker installations without the --add-host flag), you can use the host machine's actual IP address. On Linux, you can find this with:
ip addr show | grep inet | grep -v 127.0.0.1
Then set the URL to:
http://192.168.x.x:11434/
Replace 192.168.x.x with your host's IP. This is less portable but works as a fallback.
When to Use Each Solution
- host.docker.internal: Use this as the primary solution. It works out of the box on Docker Desktop (Windows, macOS) and on Linux with Docker 20.10+ (requires
docker run --add-host host.docker.internal:host-gateway). It is the cleanest approach. - Host IP address: Use this if
host.docker.internalis not available or you cannot modify the Docker run command to add the host mapping. It is more brittle because the IP may change if the host network configuration changes.
Community-Reported Caveats
-
Some users on the referenced GitHub issue (Ollama Issue #703) report that
host.docker.internalmay not work on Linux if Docker is not configured with thehost-gatewayoption. In that case, you need to add--add-host host.docker.internal:host-gatewayto yourdocker runcommand or in thedocker-compose.ymlunderextra_hosts:services: autogpt: extra_hosts: - "host.docker.internal:host-gateway" -
On Windows, if you are using WSL 2,
host.docker.internalresolves to the Windows host, not the WSL 2 Linux VM. This can cause confusion if Ollama is running inside WSL 2. In that case, use the WSL 2 VM's IP address (found viaip addr showinside WSL 2). -
The error
[Errno 111] Connection refusedcan also appear if Ollama is bound to a different port or interface. By default, Ollama listens on127.0.0.1:11434. If you changed the port or bound it to a different interface (e.g.,0.0.0.0), adjust the URL accordingly.
Common Pitfalls
Mistake 1: Forgetting to Restart Containers
After changing the configuration, you must restart the AutoGPT container. Simply editing the .env file is not enough. Run docker-compose down followed by docker-compose up -d to pick up the new environment variables.
Mistake 2: Using localhost in docker-compose.yml
If you define the Ollama service in the same docker-compose.yml file, you can use the service name (e.g., ollama) as the hostname. But if Ollama runs on the host (outside Docker), you must use host.docker.internal or the host IP. Do not use localhost.
Mistake 3: Firewall Blocking the Port
Even with the correct hostname, a firewall on the host machine may block port 11434 from Docker containers. Ensure that the host firewall allows inbound connections on port 11434 from the Docker network (typically 172.17.0.0/16).
Mistake 4: Ollama Not Listening on All Interfaces
By default, Ollama binds to 127.0.0.1. This means it only accepts connections from the host itself, not from Docker containers. To allow connections from containers, start Ollama with:
OLLAMA_HOST=0.0.0.0 ollama serve
Or set the environment variable OLLAMA_HOST=0.0.0.0 in your systemd service file or shell profile.
Related Questions
How do I check if Ollama is accessible from inside a Docker container?
Run a temporary container with curl to test connectivity:
docker run --rm curlimages/curl curl http://host.docker.internal:11434/
If you get Ollama is running, the connection works. If you get Connection refused, check that Ollama is running on the host and listening on all interfaces (0.0.0.0).
Can I run both Ollama and AutoGPT in the same Docker Compose file?
Yes. Define both services in docker-compose.yml. AutoGPT can then reach Ollama using the service name ollama (e.g., http://ollama:11434/). This avoids the host.docker.internal workaround entirely. Example:
services:
ollama:
image: ollama/ollama
ports:
- "11434:11434"
autogpt:
image: significantgravitas/auto-gpt
depends_on:
- ollama
environment:
- OLLAMA_BASE_URL=http://ollama:11434/
What does the error [Errno 111] Connection refused mean in Docker?
It means the client (AutoGPT) tried to open a TCP connection to the specified IP and port, but no service accepted the connection. In Docker, this usually happens because the target service is not running, the port is wrong, or the address is incorrect (e.g., using localhost when the service is on a different network namespace).
How do I make host.docker.internal work on Linux?
On Linux, Docker does not automatically add the host.docker.internal DNS entry. You must add it explicitly with the --add-host flag:
docker run --add-host host.docker.internal:host-gateway your-image
Or in docker-compose.yml:
services:
autogpt:
extra_hosts:
- "host.docker.internal:host-gateway"
This maps the host's gateway IP (usually the Docker bridge IP) to the hostname.
The #1 AI Newsletter
The most important ai updates, guides, and fixes โ one weekly email.
No spam, unsubscribe anytime. Privacy policy
Sources & References
This page was researched from 2 independent sources, combined and verified for completeness.
- 1.
- 2.Answer by efremidze (score 4, accepted)Stack Overflow ยท primary source
Related Answers
Keep exploring
Latest error solutions
Skip the manual work
Ready-made AI workflows and automation templates โ import and run instead of building from scratch.