1.8GB 内存也能跑大模型!Ollama Docker 部署完整指南
2026年1月24日 21:05
背景 为什么选择 Docker 部署? 因为直接使用命令会报错,无法运行ollama。 1. 简介 1.1 为什么使用 Docker 部署? 优势 说明 环境隔离 不污染宿主机环境,依赖问题少 一键部
Ollama 是一个开源的大模型运行工具,支持在本地运行 Llama 3、Qwen2.5、DeepSeek、Mistral 等上百款大语言模型。它通过命令行操作,简单高效,特别适合开发者快速部署和测试各类 AI 模型。
| 特性 | 说明 |
|---|---|
| 一键部署 | 一行命令完成安装和模型启动 |
| API 兼容 | 自带 OpenAI 格式 API,现有项目可直接迁移 |
| 跨平台支持 | macOS、Linux、Windows 全平台覆盖 |
| 模型丰富 | 内置 Qwen2.5、DeepSeek-V3、Llama 3 等上百款模型 |
| 安全可靠 | 支持密钥认证,修复已知安全漏洞 |
| 模型规模 | 显存要求 | 内存要求 | 推荐场景 |
|---|---|---|---|
| 3B(轻量) | 3GB+ | 8GB+ | 低配设备、快速测试 |
| 7B(推荐) | 4-6GB | 16GB+ | 日常开发、个人使用 |
| 13B(进阶) | 10-12GB | 32GB+ | 专业应用、团队协作 |
| 30B+(专业) | 24GB+ | 64GB+ | 企业部署、复杂任务 |
# 方法一:一键安装脚本(推荐)
curl -fsSL https://ollama.com/install.sh | sh
# 方法二:使用 Homebrew
brew install ollama
# 验证安装
ollama --version
# 一键安装脚本
curl -fsSL https://ollama.com/install.sh | sh
# 验证安装
ollama --version
OllamaSetup.exe
ollama --version
安装完成后,运行以下命令验证:
ollama --version
# 输出示例:ollama version is 0.12.0
安装完成后,必须先启动 Ollama 服务才能下载模型或进行对话。
方法一:启动服务(推荐)
ollama serve
保持这个终端窗口运行,然后新开一个终端窗口执行其他命令。
方法二:后台运行(macOS/Linux)
# 后台启动服务
ollama serve &
# 然后直接执行其他命令
ollama pull qwen2.5:7b
Windows 用户:
确保 Ollama 应用已经在运行(在系统托盘查看 Ollama 图标),或在 PowerShell/CMD 中执行:
ollama serve
# 测试服务是否正常运行
curl http://localhost:11434/api/tags
# 或查看已安装模型
ollama list
如果遇到 Error: ollama server not responding 错误:
ollama serve
lsof -i :11434(macOS/Linux)或 netstat -ano | findstr :11434(Windows)ollama serve
ollama --version 确认正确安装由于 Ollama 官方模型库在国内访问较慢,建议配置国内镜像加速:
# macOS / Linux
export OLLAMA_MODEL_SERVER=https://mirror.ollama.com
# Windows(PowerShell)
$env:OLLAMA_MODEL_SERVER="https://mirror.ollama.com"
macOS / Linux(推荐):
# 编辑配置文件
nano ~/.bashrc # 或 ~/.zshrc
# 添加以下内容
export OLLAMA_MODEL_SERVER=https://mirror.ollama.com
# 保存后重新加载配置
source ~/.bashrc # 或 source ~/.zshrc
Windows:
OLLAMA_MODEL_SERVER
https://mirror.ollama.com
为防止未授权访问,建议设置访问密钥:
# 设置密钥
export OLLAMA_API_KEY=your_strong_password123
# Windows PowerShell
$env:OLLAMA_API_KEY="your_strong_password123"
仅允许本地访问,避免暴露到公网:
# 绑定到本地回环地址
export OLLAMA_HOST=127.0.0.1:11434
# Windows PowerShell
$env:OLLAMA_HOST="127.0.0.1:11434"
访问 Ollama 官方模型库:ollama.com/library
常用中文模型推荐:
| 模型 | 说明 | 显存占用 |
|---|---|---|
qwen2.5:7b |
通义千问 2.5,中文效果优异 | ~4.5GB |
qwen2.5:14b |
更强中文能力,适合专业场景 | ~9GB |
deepseek-r1:7b |
DeepSeek 推理模型 | ~4.5GB |
gemma2:9b |
Google 开源模型 | ~5.5GB |
llama3.1:8b |
Meta Llama 3.1 | ~5GB |
# 下载通义千问 7B(中文推荐)
ollama pull qwen2.5:7b
# 下载 INT4 量化版本(显存优化)
ollama pull qwen2.5:7b-chat-q4_0
# 下载 Llama 3.1
ollama pull llama3.1:8b
# 下载 DeepSeek 推理模型
ollama pull deepseek-r1:7b
ollama list
输出示例:
NAME ID SIZE MODIFIED
qwen2.5:7b 846a0b7e 4.7GB 2 hours ago
llama3.1:8b a7872503 4.9GB 1 day ago
# 删除指定模型
ollama rm qwen2.5:7b
# 删除多个模型
ollama rm llama3.1:8b gemma2:9b
# 启动交互式对话
ollama run qwen2.5:7b
# 直接提问(非交互模式)
ollama run qwen2.5:7b "请用 Python 写一个快速排序"
# 从文件读取提示
ollama run qwen2.5:7b "$(cat prompt.txt)"
# 启动服务(默认端口 11434)
ollama serve
# 自定义端口
export OLLAMA_HOST=0.0.0.0:8080
ollama serve
# 测试健康检查
curl http://localhost:11434/api/tags
# 测试对话接口
curl http://localhost:11434/api/generate -d '{
"model": "qwen2.5:7b",
"prompt": "你好,请介绍一下你自己"
}'
// 流式响应示例
async function chatWithOllama(prompt) {
const response = await fetch("http://localhost:11434/v1/chat/completions", {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": "Bearer your_api_key"
},
body: JSON.stringify({
model: "qwen2.5:7b",
messages: [{ role: "user", content: prompt }],
stream: true
})
});
const reader = response.body.getReader();
const decoder = new TextDecoder();
let result = "";
while (true) {
const { done, value } = await reader.read();
if (done) break;
const chunk = decoder.decode(value);
const lines = chunk.split("\n").filter(line => line.trim());
for (const line of lines) {
if (line.startsWith("data: ")) {
const data = line.slice(6);
if (data === "[DONE]") continue;
try {
const json = JSON.parse(data);
const content = json.choices?.[0]?.delta?.content;
if (content) {
result += content;
console.log(content);
}
} catch (e) {
// 忽略解析错误
}
}
}
}
return result;
}
// 使用
chatWithOllama("写一个 Python 快速排序");
const axios = require("axios");
async function chatWithOllama(prompt) {
const response = await axios.post(
"http://localhost:11434/v1/chat/completions",
{
model: "qwen2.5:7b",
messages: [{ role: "user", content: prompt }],
temperature: 0.7,
max_tokens: 2000
},
{
headers: {
"Authorization": "Bearer your_api_key"
}
}
);
return response.data.choices[0].message.content;
}
// 使用
chatWithOllama("写一个 Python 快速排序").then(console.log);
解决方案:
# 配置国内镜像
export OLLAMA_MODEL_SERVER=https://mirror.ollama.com
解决方案:
# 选择 INT4 量化版本
ollama pull qwen2.5:7b-chat-q4_0
# 或选择更小的模型
ollama pull qwen2.5:3b
解决方案:
下载并安装 Microsoft C++ 生成工具: visualstudio.microsoft.com/visual-cpp-…
检查清单:
# 1. 确认服务正在运行
ollama serve
# 2. 检查端口是否被占用
lsof -i :11434 # macOS/Linux
netstat -ano | findstr :11434 # Windows
# 3. 检查防火墙设置
# 4. 验证 API 密钥
echo $OLLAMA_API_KEY
优化方案:
创建 Modelfile 自定义模型:
FROM qwen2.5:7b
# 设置系统提示
SYSTEM You are a helpful AI assistant specialized in Python programming.
# 设置参数
PARAMETER temperature 0.7
PARAMETER num_ctx 4096
构建自定义模型:
ollama create my-python-assistant -f Modelfile
ollama run my-python-assistant
调整并发请求数:
export OLLAMA_NUM_PARALLEL=4
# 设置日志级别(debug/info/warn/error)
export OLLAMA_DEBUG=1
export OLLAMA_LOG_LEVEL=info
# macOS / Linux
ollama update
# Windows:重新下载安装包覆盖安装
macOS:
brew uninstall ollama
rm -rf ~/.ollama
Linux:
sudo systemctl stop ollama
sudo systemctl disable ollama
rm -rf /usr/local/bin/ollama ~/.ollama
Windows:
.ollama 文件夹