大模型本地部署 · 收录于 2026-08-30 · 适用 2 工具 ✓ 实测验证

Docker 里跑 Open WebUI 连不上 Ollama(页面显示 Ollama 未连接 / 模型列表为空)

已验证 2026-08-30容器里 Open WebUI 连不上 Ollama——重建容器指到宿主机 Ollama,Linux 显式加 --add-host host.docker.internal:host-gateway。

快速修复

# 一键:重建容器,指到宿主机 Ollama + Linux 显式加 host-gateway 映射
docker rm -f open-webui
docker run -d --name open-webui --restart always -p 3000:8080 \
  --add-host host.docker.internal:host-gateway \
  -e OLLAMA_BASE_URLS=http://host.docker.internal:11434 \
  -v open-webui:/app/backend/data \
  ghcr.nju.edu.cn/open-webui/open-webui:main

你会遇到什么

用 Docker 部署 Open WebUI 后,页面顶部一直提示 Ollama 未连接,模型下拉是空的;docker exec 进容器访问 localhost:11434 报 Connection refused。最常见的起因:照着"本机安装教程"把 OLLAMA_BASE_URL 配成了 http://localhost:11434——在容器里这是错的。

本文路径:快速修复(30 秒一段命令)→ 一步步排查(可跳读)→ 原理与变体(症状/原因/证据)。赶时间直接跳到「快速修复」。

为什么会这样

Open WebUI 跑在 Docker 容器里,容器内的 localhost 是容器自己,不是宿主机。Ollama 默认监听宿主机 127.0.0.1:11434,容器里 localhost:11434 上没有任何服务 → 拒绝连接(实测:Errno 111 Connection refused)。要访问宿主机的 Ollama,必须用容器网络视角下宿主机的地址:

环境宿主机地址说明
Docker Desktop(Windows/macOS)host.docker.internal默认可用
Linux Docker 引擎host.docker.internal需 --add-host host.docker.internal:host-gateway 映射(本机 Docker 29.1.3 实测默认已映射,显式加无害)
docker composehost.docker.internal用 extra_hosts: ["host.docker.internal:host-gateway"]

注意新版 Open WebUI 用复数环境变量 OLLAMA_BASE_URLS(旧版是单数 OLLAMA_BASE_URL),配错变量名同样连不上。

Open WebUI 在容器里,Ollama 在宿主机——「容器内 localhost 是容器自己」这条网络命名空间规则(见 docker-host-access)在这里直接表现为 Connection refused。

配置层还有一个版本演进坑:新版 Open WebUI 的配置键是复数 OLLAMA_BASE_URLS(旧版单数 OLLAMA_BASE_URL)——变量名错了,配置静默不生效,界面照样报连不上。

确定性推论:连不上先分清是「寻址层」还是「配置键层」——容器内 curl http://host.docker.internal:11434 通 = 寻址没问题,问题在 Open WebUI 的配置键/值(URL 是否带 http://、键名是否复数);

Docker Desktop 默认注入 host.docker.internal,Linux 引擎需 --add-host host.docker.internal:host-gateway;改完环境变量要重启容器(env 只在启动时读)。

一步步来

1. 先确认是"容器内地址"问题(30 秒定位) — 必做(先确认是不是这个坑,再动手)

docker exec open-webui python3 -c "import urllib.request
for u in ['http://localhost:11434/api/version', 'http://host.docker.internal:11434/api/version']:
    try: print(u, '-> OK:', urllib.request.urlopen(u, timeout=5).read.decode.strip)
    except Exception as e: print(u, '-> FAIL:', str(e)[:60])"

实测输出形态:localhost:11434 -> FAIL: Connection refused,host.docker.internal:11434 -> OK: {"version":"0.32.14"}。命中即本坑。

2. 检查容器启动参数 — 排查(看是否漏了 --add-host / 变量名)

docker inspect open-webui 看 Config.Env 里 OLLAMA_BASE_URLS 是否指向 localhost,HostConfig.ExtraHosts 是否有 host-gateway 映射(Linux 引擎)。

3. 按上表修正并重建容器 — 必做(改完重建才生效)

  • 值:http://host.docker.internal:11434(不要带尾斜杠);
  • 变量名:新版用 OLLAMA_BASE_URLS;
  • Linux:加 --add-host host.docker.internal:host-gateway;compose 用 extra_hosts。

4. 验证 — 必做(容器内外各验一次)

  • 容器内视角:host.docker.internal:11434 返回 Ollama 版本号(实测通过);
  • 页面:刷新后模型下拉出现宿主机 Ollama 的模型列表(工具侧联调实测:open-webui 界面发现 ollama 模型通过)。

怎么确认修好了

docker exec open-webui python3 -c "import urllib.request; print(urllib.request.urlopen('http://host.docker.internal:11434/api/tags', timeout=5).read[:200])" 返回模型列表;

页面模型下拉能看到宿主机的模型(如 qwen2.5-0.5b、nomic-embed-text)。

反向场景:外部设备 / 其他容器连不上 Ollama

镜像坑:页面里地址写对了,但 Ollama 服务本身只监听 127.0.0.1——局域网里的手机/另一台机器、别的容器、远程 IDE 全都连不上(报 connection refused)。Ollama 默认只绑本机回环,要对外提供服务必须改监听地址:

# 服务侧:监听所有网卡(局域网 / 容器网络可访问)
OLLAMA_HOST=0.0.0.0:11434 ollama serve
# systemd 安装:/etc/systemd/system/ollama.service 的 [Service] 加
#   Environment="OLLAMA_HOST=0.0.0.0:11434"
# Docker 安装:启动加 -e OLLAMA_HOST=0.0.0.0:11434

改完从外部验证:curl http://<宿主机IP>:11434/api/version 返回 {"version":"..."} 即对外可达。

注意两点:监听 0.0.0.0 = 对局域网开放 API(ollama 默认无鉴权,任何人能拉模型列表/调推理,生产环境别裸奔);旧版本曾有回归只监听 localhost(ollama/ollama#12053,v0.11.5/0.11.6 已修复),升版也是一种解法。

其它环境的表现

环境说明
变量名新版 OLLAMA_BASE_URLS(复数);旧教程写 OLLAMA_BASE_URL(单数)可能不生效
Linux 引擎host.docker.internal 需要 --add-host host.docker.internal:host-gateway;本机 Docker 29.1.3 实测默认已映射,显式加无害
docker composeenvironment + extra_hosts: ["host.docker.internal:host-gateway"]
UI 表现Open WebUI 对 Ollama 是懒连接,匿名请求/日志不一定能看到报错;以容器内实测为准
未实测非 Linux 平台(Docker Desktop win/mac 的 host.docker.internal 默认行为)未在本机复现,按官方机制标注

症状

Docker 部署的 Open WebUI 页面提示 Ollama 未连接、模型列表为空;容器内访问 localhost:11434 拒绝连接。

原因

容器内 localhost 是容器自己,不是宿主机;OLLAMA_BASE_URLS 指向 localhost:11434 时连不到宿主机的 Ollama。正确做法是用 host.docker.internal(Linux 引擎需 host-gateway 映射)。

解法

  1. docker exec 容器内实测两个地址,确认 localhost 拒绝、host.docker.internal 可达;
  2. 重建容器:-e OLLAMA_BASE_URLS=http://host.docker.internal:11434(新版用复数变量名);
  3. Linux 引擎加 --add-host host.docker.internal:host-gateway(compose 用 extra_hosts);
  4. 刷新页面确认模型列表出现。
验证证据
  • 本机实测(2026-08-30,WSL2 Ubuntu + Docker 29.1.3 + ollama 0.32.14):独立容器(端口 3001 + 独立 volume,未碰工具侧容器)内 localhost:11434 → Errno 111 Connection refused;host.docker.internal:11434 → {"version":"0.32.14"}。/api/models 匿名请求 401(懒连接 + 需登录会话),UI 未连接横幅未截获,机制层面已坐实。证据:实测复现日志(2026-08-30)。
  • 工具侧联调证据(2026-08-30,实测复现日志(2026-08-30)):--add-host=host.docker.internal:host-gateway + OLLAMA_BASE_URLS=http://host.docker.internal:11434 容器健康,docker exec 访问 /api/tags 通过,open-webui 界面发现 ollama 模型。
  • 未实测:Docker Desktop(Windows/macOS)的 host.docker.internal 默认行为(机制由官方文档标注)。

相关 canonical

按「前置 → 同根 / 同域 / 相关」读:

适用工具 (2)

本问题方案已挂到下列工具的方案页——从工具页也能回到这里,双向可达。

open-webuidocker · 外部工具

成套方案 (组合包)

这个问题包含在下列成套安装方案中。

来源与证据

收录 验证 ✓ 实测验证适用 2 工具
方案含本地实测或来源可复核的解法;未实测项已在文中如实标注。

← 大模型本地部署 问题域 | 问题货架 | 遇到其它问题?去 问题空间 提交求助卡