Docker Model Runner 快速参考:命令与示例
Docker Model Runner 命令快速参考
Docker 模型运行器 (DMR) 是 Docker 官方用于本地运行 AI 模型的解决方案,于 2025 年 4 月推出。此速查表为所有关键命令、配置和最佳实践提供快速参考。
安装
Docker Desktop
通过图形界面启用 Docker 模型运行器:
- 打开 Docker Desktop
- 进入 设置 → AI 选项卡
- 点击 启用 Docker 模型运行器
- 重启 Docker Desktop
/home/rg/prj/hugo-pers/content/post/2025/10/docker-model-runner-cheatsheet/docker-model-runner_w678.jpg
Docker Engine (Linux)
安装插件包:
# Ubuntu/Debian
sudo apt-get update
sudo apt-get install docker-model-plugin
# Fedora/RHEL
sudo dnf install docker-model-plugin
# Arch Linux
sudo pacman -S docker-model-plugin
验证安装:
docker model --help
核心命令
拉取模型
从 Docker Hub 拉取预打包模型:
# 基础拉取
docker model pull ai/llama2
# 拉取特定版本
docker model pull ai/llama2:7b-q4
# 从自定义仓库拉取
docker model pull myregistry.com/models/mistral:latest
# 列出命名空间中的可用模型
docker search ai/
运行模型
启动模型并自动提供 API 服务:
# 基础运行(交互式)
docker model run ai/llama2 "What is Docker?"
# 作为服务运行(后台)
docker model run -d --name my-llm ai/llama2
# 使用自定义端口运行
docker model run -p 8080:8080 ai/llama2
# 使用 GPU 规格运行
docker model run --gpus 0,1 ai/llama2
# 使用内存限制运行
docker model run --memory 8g ai/llama2
# 使用环境变量运行
docker model run -e MODEL_CONTEXT=4096 ai/llama2
# 使用卷挂载运行以实现数据持久化
docker model run -v model-data:/data ai/llama2
列出模型
查看已下载和正在运行的模型:
# 列出所有已下载模型
docker model ls
# 列出正在运行的模型
docker model ps
# 以详细信息列出
docker model ls --all --format json
# 按名称过滤
docker model ls --filter "name=llama"
停止模型
停止正在运行的模型实例:
# 停止特定模型
docker model stop my-llm
# 停止所有正在运行的模型
docker model stop $(docker model ps -q)
# 带超时停止
docker model stop --time 30 my-llm
删除模型
从本地存储中删除模型:
# 删除特定模型
docker model rm ai/llama2
# 强制删除(即使正在运行)
docker model rm -f ai/llama2
# 删除未使用的模型
docker model prune
# 删除所有模型
docker model rm $(docker model ls -q)
打包自定义模型
从 GGUF 创建 OCI 软件包
打包自己的 GGUF 模型:
# 基础打包
docker model package --gguf /path/to/model.gguf myorg/mymodel:latest
# 带元数据打包
docker model package \
--gguf /path/to/model.gguf \
--label "description=自定义 Llama 模型" \
--label "version=1.0" \
myorg/mymodel:v1.0
# 一次打包并推送
docker model package --gguf /path/to/model.gguf --push myorg/mymodel:latest
# 带自定义上下文大小打包
docker model package \
--gguf /path/to/model.gguf \
--context 8192 \
myorg/mymodel:latest
发布模型
将模型推送到仓库:
# 登录到 Docker Hub
docker login
# 推送到 Docker Hub
docker model push myorg/mymodel:latest
# 推送到私有仓库
docker login myregistry.com
docker model push myregistry.com/models/mymodel:latest
# 标记并推送
docker model tag mymodel:latest myorg/mymodel:v1.0
docker model push myorg/mymodel:v1.0
API 使用
OpenAI 兼容端点
Docker 模型运行器自动暴露 OpenAI 兼容的 API:
# 启动模型并启用 API
docker model run -d -p 8080:8080 --name llm ai/llama2
# 聊天完成
curl http://localhost:8080/v1/chat/completions \
-H "Content-Type: application/json" \
-d '{
"model": "llama2",
"messages": [{"role": "user", "content": "Hello!"}]
}'
# 文本生成
curl http://localhost:8080/v1/completions \
-H "Content-Type: application/json" \
-d '{
"model": "llama2",
"prompt": "Once upon a time",
"max_tokens": 100
}'
# 流式响应
curl http://localhost:8080/v1/chat/completions \
-H "Content-Type: application/json" \
-d '{
"model": "llama2",
"messages": [{"role": "user", "content": "Tell me a story"}],
"stream": true
}'
# 通过 API 列出可用模型
curl http://localhost:8080/v1/models
# 模型信息
curl http://localhost:8080/v1/models/llama2
Docker Compose 配置
基础 Compose 文件
version: '3.8'
services:
llm:
image: docker-model-runner
model: ai/llama2:7b-q4
ports:
- "8080:8080"
environment:
- MODEL_CONTEXT=4096
- MODEL_TEMPERATURE=0.7
volumes:
- model-data:/root/.cache
deploy:
resources:
reservations:
devices:
- driver: nvidia
count: all
capabilities: [gpu]
volumes:
model-data:
多模型设置
version: '3.8'
services:
llama:
image: docker-model-runner
model: ai/llama2
ports:
- "8080:8080"
mistral:
image: docker-model-runner
model: ai/mistral
ports:
- "8081:8080"
embedding:
image: docker-model-runner
model: ai/nomic-embed-text
ports:
- "8082:8080"
有关更高级的 Docker Compose 配置和命令,请参阅我们的 Docker Compose 速查表,涵盖网络、卷和编排模式。
环境变量
使用环境变量配置模型行为:
# 上下文窗口大小
MODEL_CONTEXT=4096
# 温度(0.0-1.0)
MODEL_TEMPERATURE=0.7
# Top-p 采样
MODEL_TOP_P=0.9
# Top-k 采样
MODEL_TOP_K=40
# 最大 token 数
MODEL_MAX_TOKENS=2048
# GPU 层数
MODEL_GPU_LAYERS=35
# 批量大小
MODEL_BATCH_SIZE=512
# 线程数(CPU)
MODEL_THREADS=8
# 启用详细日志
MODEL_VERBOSE=true
# 认证的 API 密钥
MODEL_API_KEY=your-secret-key
使用环境变量运行:
docker model run \
-e MODEL_CONTEXT=8192 \
-e MODEL_TEMPERATURE=0.8 \
-e MODEL_API_KEY=secret123 \
ai/llama2
GPU 配置
自动 GPU 检测
DMR 自动检测并使用可用的 GPU:
# 使用所有 GPU
docker model run --gpus all ai/llama2
# 使用特定 GPU
docker model run --gpus 0 ai/llama2
# 使用多个特定 GPU
docker model run --gpus 0,1,2 ai/llama2
# 带内存限制的 GPU
docker model run --gpus all --memory 16g ai/llama2
仅 CPU 模式
当 GPU 可用时强制使用 CPU 推理:
docker model run --no-gpu ai/llama2
多 GPU 张量并行
将大型模型分布在多个 GPU 上:
docker model run \
--gpus all \
--tensor-parallel 2 \
ai/llama2-70b
检查和调试
查看模型详情
# 查看模型配置
docker model inspect ai/llama2
# 查看模型层
docker model history ai/llama2
# 检查模型大小和元数据
docker model inspect --format='{{.Size}}' ai/llama2
日志和监控
# 查看模型日志
docker model logs llm
# 实时跟踪日志
docker model logs -f llm
# 查看最后 100 行
docker model logs --tail 100 llm
# 查看带时间戳的日志
docker model logs -t llm
性能统计
# 资源使用情况
docker model stats
# 特定模型统计
docker model stats llm
# JSON 格式的统计
docker model stats --format json
网络
暴露 API
# 默认端口(8080)
docker model run -p 8080:8080 ai/llama2
# 自定义端口
docker model run -p 3000:8080 ai/llama2
# 绑定到特定接口
docker model run -p 127.0.0.1:8080:8080 ai/llama2
# 多个端口
docker model run -p 8080:8080 -p 9090:9090 ai/llama2
网络配置
# 创建自定义网络
docker network create llm-network
# 在自定义网络上运行模型
docker model run --network llm-network --name llm ai/llama2
# 连接到现有网络
docker model run --network host ai/llama2
安全
访问控制
# 使用 API 密钥认证运行
docker model run \
-e MODEL_API_KEY=my-secret-key \
ai/llama2
# 使用认证
curl http://localhost:8080/v1/chat/completions \
-H "Authorization: Bearer my-secret-key" \
-H "Content-Type: application/json" \
-d '{"model": "llama2", "messages": [...]}'
仓库认证
# 登录到私有仓库
docker login myregistry.com -u username -p password
# 从私有仓库拉取
docker model pull myregistry.com/private/model:latest
# 使用凭证助手
docker login --password-stdin < token.txt
最佳实践
模型选择
# 使用量化模型以加快推理速度
docker model pull ai/llama2:7b-q4 # 4 位量化
docker model pull ai/llama2:7b-q5 # 5 位量化
docker model pull ai/llama2:7b-q8 # 8 位量化
# 检查模型变体
docker search ai/llama2
资源管理
# 设置内存限制
docker model run --memory 8g --memory-swap 16g ai/llama2
# 设置 CPU 限制
docker model run --cpus 4 ai/llama2
# 限制 GPU 内存
docker model run --gpus all --gpu-memory 8g ai/llama2
健康检查
# 带健康检查运行
docker model run \
--health-cmd "curl -f http://localhost:8080/health || exit 1" \
--health-interval 30s \
--health-timeout 10s \
--health-retries 3 \
ai/llama2
生产编排
在 Kubernetes 上进行生产部署时,可以使用标准的 Kubernetes 清单对 Docker 模型运行器容器进行编排。定义带有资源限制、自动扩展和负载均衡的部署。有关全面的 Kubernetes 命令参考和部署模式,请查看我们的 Kubernetes 速查表。
# 示例:部署到 Kubernetes 集群
kubectl apply -f llm-deployment.yaml
# 扩展部署
kubectl scale deployment llm --replicas=3
# 暴露为服务
kubectl expose deployment llm --type=LoadBalancer --port=8080
故障排除
常见问题
模型无法启动:
# 检查可用磁盘空间
df -h
# 查看详细错误日志
docker model logs --tail 50 llm
# 验证 GPU 可用性
nvidia-smi # 对于 NVIDIA GPU
内存不足错误:
# 使用较小的量化模型
docker model pull ai/llama2:7b-q4
# 减少上下文大小
docker model run -e MODEL_CONTEXT=2048 ai/llama2
# 限制批量大小
docker model run -e MODEL_BATCH_SIZE=256 ai/llama2
推理速度慢:
# 检查 GPU 使用情况
docker model stats llm
# 确保使用了 GPU
docker model logs llm | grep -i gpu
# 增加 GPU 层数
docker model run -e MODEL_GPU_LAYERS=40 ai/llama2
诊断命令
# 系统信息
docker model system info
# 磁盘使用情况
docker model system df
# 清理未使用资源
docker model system prune
# 完全清理(删除所有模型)
docker model system prune -a
集成示例
Python 集成
import openai
# 配置 Docker 模型运行器客户端
client = openai.OpenAI(
base_url="http://localhost:8080/v1",
api_key="not-needed" # DMR 默认不需要密钥
)
# 聊天完成
response = client.chat.completions.create(
model="llama2",
messages=[
{"role": "user", "content": "Hello!"}
]
)
print(response.choices[0].message.content)
# 流式传输
stream = client.chat.completions.create(
model="llama2",
messages=[{"role": "user", "content": "Tell me a story"}],
stream=True
)
for chunk in stream:
if chunk.choices[0].delta.content:
print(chunk.choices[0].delta.content, end="")
Bash 脚本
#!/bin/bash
# 如果模型未运行,则启动模型
if ! docker model ps | grep -q "llm"; then
docker model run -d --name llm -p 8080:8080 ai/llama2
echo "等待模型启动..."
sleep 10
fi
# 发起 API 调用
curl -s http://localhost:8080/v1/chat/completions \
-H "Content-Type: application/json" \
-d '{
"model": "llama2",
"messages": [{"role": "user", "content": "'"$1"'"}]
}' | jq -r '.choices[0].message.content'
Node.js 集成
import OpenAI from 'openai';
const client = new OpenAI({
baseURL: 'http://localhost:8080/v1',
apiKey: 'not-needed'
});
async function chat(message) {
const completion = await client.chat.completions.create({
model: 'llama2',
messages: [{ role: 'user', content: message }]
});
return completion.choices[0].message.content;
}
// 使用
const response = await chat('Docker 模型运行器是什么?');
console.log(response);