baodan/DOCKER_OPTIMIZATION.md

242 lines
5.5 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# Docker 镜像优化指南
## 📊 当前镜像大小分析
| 镜像 | 大小 | 说明 |
|------|------|------|
| `langgenius/dify-api:latest` | **4.12GB** | Dify 官方 API 镜像 |
| `baodanagent-dify-api:latest` | **4.12GB** | 自定义 API 镜像 |
| `langgenius/dify-plugin-daemon` | **2.31GB** | 插件守护进程 |
| `langgenius/dify-web:latest` | **759MB** | Web 前端镜像 |
| `langgenius/dify-sandbox:0.2.15` | **848MB** | 代码执行沙箱 |
| **总计** | **~17.5GB** | 所有镜像 |
## 🔍 镜像大的原因
### 1. Dify 官方镜像本身就大
Dify 是一个复杂的 AI 应用平台,包含:
- Python 3.12 运行时
- PyTorch / TransformersML 框架)
- 向量数据库依赖pgvector, qdrant, weaviate
- 多种 LLM SDKOpenAI, Anthropic, 本地模型等)
- Celery 异步任务框架
- 数据库 ORM 和迁移工具
### 2. 多服务架构
系统包含 6 个独立服务,每个都有自己的镜像:
- API 服务
- Web 前端
- Worker 异步任务
- Plugin Daemon 插件守护进程
- Sandbox 代码沙箱
- 数据库和缓存
## ✅ 已完成的优化
### 1. Dockerfile 优化
```dockerfile
# 清理不必要的文件
RUN apt-get clean && \
rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* && \
pip cache purge 2>/dev/null || true && \
rm -rf /root/.cache/pip
```
### 2. .dockerignore 优化
创建了 `.dockerignore` 文件,排除:
- 文档文件docs/
- 前端源码frontend/
- 测试文件
- 虚拟环境
- 缓存文件
### 3. 构建缓存优化
使用 Docker BuildKit 缓存:
```bash
DOCKER_BUILDKIT=1 docker build --cache-from ...
```
## 🚀 进一步优化方案
### 方案 1清理未使用的镜像
```bash
# 查看未使用的镜像
docker images -f "dangling=true"
# 清理悬空镜像
docker image prune -f
# 清理所有未使用的镜像(谨慎使用)
docker image prune -a -f
```
### 方案 2使用精简基础镜像
如果不需要所有 Dify 功能,可以使用 Alpine 版本:
```dockerfile
# 替换
FROM langgenius/dify-api:latest
# 为(如果官方提供)
FROM langgenius/dify-api:alpine
```
### 方案 3多阶段构建
```dockerfile
# 构建阶段
FROM python:3.12-slim as builder
COPY requirements.txt .
RUN pip install --user -r requirements.txt
# 运行阶段
FROM python:3.12-slim
COPY --from=builder /root/.local /root/.local
COPY api/insurance /app/api/insurance
```
### 方案 4禁用不需要的服务
如果不需要某些功能,可以在 `docker-compose.dify.yml` 中注释掉:
```yaml
# 如果不需要代码沙箱
# sandbox:
# image: langgenius/dify-sandbox:0.2.15
# ...
# 如果不需要插件守护进程
# plugin_daemon:
# image: langgenius/dify-plugin-daemon:0.6.1-local
# ...
```
## 🛠️ 磁盘清理工具
### 使用清理脚本
```bash
# 运行清理脚本
./docker-cleanup.sh
```
### 手动清理命令
```bash
# 1. 查看 Docker 磁盘使用
docker system df
# 2. 清理停止的容器
docker container prune -f
# 3. 清理悬空镜像
docker image prune -f
# 4. 清理构建缓存
docker builder prune -f
# 5. 清理所有未使用资源(谨慎)
docker system prune -a -f
# 6. 清理所有未使用资源包括卷(非常谨慎)
docker system prune -a -f --volumes
```
## 📈 优化效果对比
| 优化措施 | 预计节省 | 说明 |
|---------|---------|------|
| 清理构建缓存 | 8-10GB | 一次性释放 |
| 清理悬空镜像 | 300-500MB | 定期清理 |
| 禁用不需要的服务 | 2-3GB | 按需裁剪 |
| 使用 .dockerignore | 50-100MB | 每次构建 |
## 🎯 推荐优化步骤
### 第一步:立即释放空间
```bash
# 清理构建缓存(最大收益)
docker builder prune -f --keep-storage 2GB
# 清理悬空镜像
docker image prune -f
```
### 第二步:评估服务需求
根据实际需求决定保留哪些服务:
| 服务 | 必要性 | 说明 |
|------|:------:|------|
| baodan-api | ✅ 必须 | 核心 API 服务 |
| baodan-web | ✅ 必须 | 管理后台 |
| baodan-worker | ⚠️ 按需 | 异步任务(可选) |
| db | ✅ 必须 | 数据库 |
| redis | ✅ 必须 | 缓存 |
| plugin_daemon | ⚠️ 按需 | 插件功能(可选) |
| sandbox | ⚠️ 按需 | 代码执行(可选) |
### 第三步:优化 Dockerfile
已优化的 Dockerfile 包含:
- 清理 apt 缓存
- 清理 pip 缓存
- 删除临时文件
### 第四步:定期维护
```bash
# 添加到 crontab每周清理一次
0 2 * * 0 cd /path/to/project && docker system prune -f >> /var/log/docker-cleanup.log 2>&1
```
## 📝 注意事项
1. **不要删除正在使用的镜像**:先停止容器再删除镜像
2. **谨慎使用 `--volumes`**:会删除所有未挂载的数据卷
3. **备份重要数据**:清理前确保数据已备份
4. **保留基础镜像**:避免重复下载官方镜像
## 🔗 相关命令参考
```bash
# 查看镜像详情
docker inspect <image_id>
# 查看镜像层
docker history <image_id>
# 导出镜像(备份)
docker save -o baodan-api.tar baodanagent-dify-api:latest
# 导入镜像(恢复)
docker load -i baodan-api.tar
# 推送到私有仓库
docker tag baodanagent-dify-api:latest your-registry.com/baodan-api:latest
docker push your-registry.com/baodan-api:latest
```
## 📊 最终优化效果
经过优化后,预计磁盘使用:
| 项目 | 优化前 | 优化后 | 节省 |
|------|--------|--------|------|
| 镜像大小 | 17.5GB | 10-12GB | 5-7GB |
| 构建缓存 | 10.3GB | 2GB | 8GB |
| **总计** | **~28GB** | **~14GB** | **~14GB** |
---
**优化完成时间**: 2026-06-19
**优化状态**: ✅ 已完成基础优化