How to Fix Ollama Connection Refused from Docker on Linux
LLM Serving(Updated )

How to Fix Ollama Connection Refused from Docker on Linux

Docker cannot reach Ollama on localhost: diagnose the network boundary first

A Docker container may report ConnectionRefusedError: [Errno 111] Connection refused when it calls an Ollama server running on the Linux host. A common configuration possibility to investigate is that 127.0.0.1 inside a bridged container refers to the container, while a default host-installed Ollama listens on the host loopback interface. This is a network-namespace distinction, not a measured ranking of error causes.

Security correction (November 11, 2026): An earlier version of this guide suggested binding Ollama to 0.0.0.0:11434 and applying several broad UFW allow/deny rules as a general fix. That advice could expose an unauthenticated local model API to other networks and did not establish a reliable firewall boundary. We have removed it. The safer choice depends on whether the client is a trusted local process, whether Ollama runs on the host or in Docker, and which Docker networks are connected.

1. Confirm that Ollama is actually running

Start with service-status and an HTTP GET check on the host. The official Ollama model-list endpoint documents GET /api/tags. These commands may fail when Ollama is absent or the system does not use systemd, and were not executed on a target Docker host for this article:

systemctl status ollama --no-pager
ss -ltnp | grep 11434
curl --max-time 3 -fsS http://127.0.0.1:11434/api/tags

The Ollama FAQ documents that a default local server binds to 127.0.0.1:11434 unless its configuration changes. If the local host request fails, fix the Ollama service first; changing a Docker hostname will not make a stopped service respond. Read the official Ollama FAQ for the supported OLLAMA_HOST option.

2. Choose the appropriate connection pattern

Pattern A: Ollama and the client both run in Docker. Prefer connecting them to a dedicated user-defined Docker bridge. A client can then address http://ollama:11434 by Compose service name, and there is no need to publish port 11434 to the host’s LAN.

A minimal unexecuted Compose networking illustration, not a complete deployment package. The official Ollama Docker page documents the image family; the exact image tag and client env-variable behavior must be validated for your chosen software:

services:
  ollama:
    image: ollama/ollama:YOUR_TESTED_TAG
    environment:
      OLLAMA_HOST: "0.0.0.0:11434"  # Only inside this isolated container namespace
    networks: [model_network]
    # No ports: entry. Any peer connected to model_network may reach this listener.
    # Control network membership and evaluate authentication separately.

  client:
    image: YOUR_APPROVED_CLIENT_IMAGE
    environment:
      OLLAMA_BASE_URL: http://ollama:11434
    networks: [model_network]

networks:
  model_network:
    driver: bridge

The explicit container-side OLLAMA_HOST ensures the service is intended to listen on interfaces reachable from peer containers, not only its own loopback address. Unlike binding host Ollama on 0.0.0.0, this container setting does not itself publish a host port; access still depends on the actual Docker networks connected. Other containers on that network can reach it, so use a dedicated trusted network and check the image’s installed Ollama version and effective listener. The official Ollama FAQ documents this binding setting. This image/configuration combination was not tested in a live container during this editorial update. The setting is an illustration of intended addressing, not verified support for every image tag.

Replace the image placeholders with versions that you have reviewed. This example leaves out model storage, GPU allocation, volumes, application authentication, resource limits and lifecycle details; it is not a copy-and-run production configuration. In a production environment, also review the containers’ capabilities and network membership. Containers on that bridge can reach the service even without a published port.

Pattern B: Ollama stays on the Linux host; the client is a trusted, short-lived local container. Docker host networking can allow a container to reach a host-local listener without rebinding Ollama. The next command is an unexecuted test-container example, not a verified repair. It can pull an image and starts a process, so run it only on an authorized, disposable test environment:

# Only for a client you trust, because --network host shares the host network namespace.
docker run --rm --network host python:3.12-slim \
  python -c "import urllib.request; print(urllib.request.urlopen('http://127.0.0.1:11434/api/tags', timeout=3).status)"

This removes the container’s ordinary network separation. Do not use it as a general security recommendation for untrusted AI coding agents or plugins.

Pattern C: Ollama stays on the host; the client uses an ordinary Docker bridge. Docker can map host.docker.internal to the default bridge gateway through --add-host=host.docker.internal:host-gateway. But that name only identifies a host address; it does not change where Ollama is listening. The exact host-gateway behavior is documented in Docker container-run reference. A host service bound only to loopback usually cannot be reached via the bridge gateway.

On a conventional rootful Docker installation, first check the actual bridge IPv4 address:

ip -4 addr show docker0
docker network inspect bridge --format '{{(index .IPAM.Config 0).Gateway}}'

If the site-specific access policy allows it, a system administrator can consider binding only to the verified bridge interface IP, rather than 0.0.0.0. For example, a host with bridge address 172.17.0.1 could use an Ollama systemd drop-in setting of OLLAMA_HOST=172.17.0.1:11434. Do not copy this address without checking the actual host. Rootless Docker, custom bridge networks, IPv6 and firewalls need separate handling. Binding to the bridge address also means that old host-loopback clients may need an updated endpoint.

If host.docker.internal is correctly configured, the following is an unexecuted, authorized container networking probe. It may download the image, starts a short-lived container and makes an HTTP request, so it is not a read-only host-state check. Confirm the selected image, listener and network policy first:

docker run --rm --add-host=host.docker.internal:host-gateway \
  python:3.12-slim \
  python -c "import urllib.request; print(urllib.request.urlopen('http://host.docker.internal:11434/api/tags', timeout=3).status)"

The hostname mapping is documented by Docker’s container-run reference. For Docker Compose, use extra_hosts with host-gateway.

3. Do not substitute a few UFW commands for an exposure review

OLLAMA_HOST=0.0.0.0:11434 listens on all IPv4 interfaces, subject to network and firewall policy. The official Ollama authentication reference states that the local HTTP API does not require authentication. If you expose it beyond a trusted local context, add and verify an independent access-control boundary. Its cloud API authentication rules are separate and should not be mistaken for local-server protection. On a workstation or server that also runs containers, the consequences of such a bind depend on actual listening addresses, routing, host firewall rules and Docker’s network rules.

In particular, Docker-published container ports can bypass expectations built around UFW’s ordinary firewall rules. Docker’s Ubuntu installation security guidance and packet-filtering documentation explain this interaction. Those documents do not imply that every host-bound Ollama request automatically bypasses UFW; the exact packet path matters.

The old advice to allow entire 172.17.0.0/16 and 172.18.0.0/16 address ranges, then broadly deny port 11434, should not be used as a universal fix. Docker bridges may have different subnets and exposure paths, and rule ordering and network interfaces matter. Have the host administrator review the actual firewall policy instead of pasting generic privileged commands.

For a service intended to be accessed from outside a trusted machine, use a deliberately designed access boundary such as an authenticated reverse proxy or an approved private-network solution, then test authorization and exposure independently.

4. Diagnose the failure without guessing

  • The host’s own curl fails: inspect Ollama service status, bind address, and host logs.
  • The host works; bridged container gets connection refused: check the container’s destination address and the host’s listening interface.
  • A hostname cannot resolve: inspect --add-host / Compose extra_hosts and the actual bridge gateway.
  • A request times out rather than being refused: investigate routing and firewall rules on the specific path; do not assume UFW is solely responsible.
  • The endpoint works from an unintended network: treat that as an access-control concern, not an installation success.
  • You need a coding agent with isolated networking: prefer an intentionally designed service boundary over --network host.

Record the container’s network mode, the host interface and listener address, the Docker Engine version, and the service logs. These details distinguish connection, routing, and access-control problems far better than an unconditional firewall recipe.

References

Independently source-reviewed November 12, 2026. These are conditional troubleshooting patterns; no live Docker image or host firewall configuration was tested for this article. No complete deployment environment is distributed.

BC

Written by B. Cho

Systems & AI Infrastructure Engineer

Specializing in GPU inference engines (vLLM, SGLang, Ollama), CUDA memory allocation, and Model Context Protocol (MCP) infrastructure on Linux workstations.