Docker 部署指南

Crawl4AI 提供官方 Docker 镜像,方便部署和扩展。本指南介绍在 Docker 环境中的安装、配置和使用方法。

快速开始 🚀

拉取并运行基础版本:

  1. # 运行无安全配置的基础版本
  2. docker pull unclecode/crawl4ai:basic
  3. docker run -p 11235:11235 unclecode/crawl4ai:basic
  4. # 运行启用 API 安全的版本
  5. docker run -p 11235:11235 -e CRAWL4AI_API_TOKEN=your_secret_token unclecode/crawl4ai:basic

使用 Docker Compose 运行 🐳

使用 Docker Compose(本地构建或 Docker Hub)

Crawl4AI 提供灵活的 Docker Compose 方案来管理容器化服务。你可以选择从提供的 Dockerfile 本地构建镜像,或者直接使用 Docker Hub 上的预构建镜像。

方案 1:使用 Docker Compose 本地构建

如果希望从本地 Dockerfile 构建镜像,请使用 docker-compose.local.yml 文件:

  1. docker-compose -f docker-compose.local.yml up -d

该命令将执行以下操作:

  1. Dockerfile 构建 Docker 镜像。
  2. 启动容器,并在 http://localhost:11235 提供服务。

方案 2:使用 Docker Hub 上的预构建镜像

如果你想直接使用 Docker Hub 上的官方镜像,请使用 docker-compose.hub.yml 文件:

  1. docker-compose -f docker-compose.hub.yml up -d

该命令将执行以下操作:

  1. 拉取 unclecode/crawl4ai:basic(或 all 版本,取决于你的配置)。
  2. 启动容器,并在 http://localhost:11235 提供服务。

停止运行的服务

要停止通过 Docker Compose 启动的服务,可以使用以下命令:

  1. docker-compose -f docker-compose.local.yml down
  2. # 或
  3. docker-compose -f docker-compose.hub.yml down

如果容器没有成功停止,应用仍在运行,可以检查正在运行的容器:

  1. docker ps

找到 CONTAINER ID 并强制停止:

  1. docker stop <CONTAINER_ID>

使用 Docker Compose 进行调试

  • 查看日志
  1. docker-compose -f docker-compose.local.yml logs -f
  • 移除孤立容器(如果应用仍在意外运行):
  1. docker-compose -f docker-compose.local.yml down --remove-orphans
  • 手动移除 Docker 网络(如果网络仍被占用):
  1. docker network ls
  2. docker network rm crawl4ai_default

为什么使用 Docker Compose?

Docker Compose 是推荐的部署方式,因为:

  1. 简化了多容器服务的管理。
  2. 允许在单个文件中定义环境变量、资源和端口。
  3. 便于在本地开发与生产环境之间切换。

例如,你的 docker-compose.yml 可以包含 API 密钥、令牌设置和内存限制,使部署快速且一致。

API 安全 🔒

了解 CRAWL4AI_API_TOKEN

CRAWL4AI_API_TOKEN 用于保护 Crawl4AI 实例:

  • 设置 CRAWL4AI_API_TOKEN:所有 API 端点(除 /health 以外)均需要身份验证。
  • 未设置 CRAWL4AI_API_TOKEN:API 端点将公开访问。
  1. # 启用安全性的实例
  2. docker run -p 11235:11235 -e CRAWL4AI_API_TOKEN=your_secret_token unclecode/crawl4ai:all
  3. # 无安全性的实例
  4. docker run -p 11235:11235 unclecode/crawl4ai:all

API 调用示例

对于启用了安全性的实例,请在所有请求中包含 API 令牌:

  1. import requests
  2. # 设置请求头
  3. api_token = "your_secret_token" # 与 CRAWL4AI_API_TOKEN 保持一致
  4. headers = {"Authorization": f"Bearer {api_token}"} if api_token else {}
  5. # 发送爬取任务请求
  6. response = requests.post(
  7. "http://localhost:11235/crawl",
  8. headers=headers,
  9. json={
  10. "urls": "https://example.com",
  11. "priority": 10
  12. }
  13. )
  14. # 查询任务状态
  15. task_id = response.json()["task_id"]
  16. status = requests.get(
  17. f"http://localhost:11235/task/{task_id}",
  18. headers=headers
  19. )

配置选项 🔧

环境变量

可以使用环境变量配置 Crawl4AI 服务:

  1. # 基础配置
  2. docker run -p 11235:11235 \
  3. -e MAX_CONCURRENT_TASKS=5 \
  4. unclecode/crawl4ai:all
  5. # 启用安全性与 LLM 支持
  6. docker run -p 11235:11235 \
  7. -e CRAWL4AI_API_TOKEN=your_secret_token \
  8. -e OPENAI_API_KEY=sk-... \
  9. -e ANTHROPIC_API_KEY=sk-ant-... \
  10. unclecode/crawl4ai:all

使用 Docker Compose(推荐) 🐳

创建 docker-compose.yml

  1. version: '3.8'
  2. services:
  3. crawl4ai:
  4. image: unclecode/crawl4ai:all
  5. ports:
  6. - "11235:11235"
  7. environment:
  8. - CRAWL4AI_API_TOKEN=${CRAWL4AI_API_TOKEN:-} # 可选的 API 令牌
  9. - MAX_CONCURRENT_TASKS=5
  10. # LLM 提供商密钥
  11. - OPENAI_API_KEY=${OPENAI_API_KEY:-}
  12. - ANTHROPIC_API_KEY=${ANTHROPIC_API_KEY:-}
  13. volumes:
  14. - /dev/shm:/dev/shm
  15. deploy:
  16. resources:
  17. limits:
  18. memory: 4G
  19. reservations:
  20. memory: 1G

运行方式:

  1. 使用环境变量直接运行
  1. CRAWL4AI_API_TOKEN=secret123 OPENAI_API_KEY=sk-... docker-compose up
  1. 使用 .env 文件(推荐)

docker-compose.yml 文件同级目录创建 .env 文件:

  1. # API 安全令牌(可选)
  2. CRAWL4AI_API_TOKEN=your_secret_token
  3. # LLM 提供商密钥
  4. OPENAI_API_KEY=sk-...
  5. ANTHROPIC_API_KEY=sk-ant-...
  6. # 其他配置
  7. MAX_CONCURRENT_TASKS=5

然后运行:

  1. docker-compose up

安全提醒:如果启用了 API 令牌,请确保其安全,避免将其提交到版本控制系统中。

平台专属安装指引 💻

macOS

  1. docker pull unclecode/crawl4ai:basic
  2. docker run -p 11235:11235 unclecode/crawl4ai:basic

Ubuntu

  1. # 运行基础版本
  2. docker pull unclecode/crawl4ai:basic
  3. docker run -p 11235:11235 unclecode/crawl4ai:basic
  4. # 运行支持 GPU 版本
  5. docker pull unclecode/crawl4ai:gpu
  6. docker run --gpus all -p 11235:11235 unclecode/crawl4ai:gpu

Windows(PowerShell)

  1. docker pull unclecode/crawl4ai:basic
  2. docker run -p 11235:11235 unclecode/crawl4ai:basic

生产环境部署 🚀

在生产环境中运行 Crawl4AI 时,建议使用以下方法:

  1. 使用 Docker Compose 进行容器管理。
  2. 启用 API 令牌 以防止未经授权的访问。
  3. 使用反向代理(如 Nginx 或 Traefik) 来提供 HTTPS 支持。
  4. 配置持久化存储,避免数据丢失。

生产环境推荐配置(Nginx + Docker Compose)

在生产环境中,建议使用 Nginx 作为反向代理,同时使用 Docker Compose 来管理容器。

步骤 1:安装 Nginx

  1. sudo apt update && sudo apt install -y nginx

步骤 2:配置 Nginx 作为反向代理

创建 Nginx 配置文件 /etc/nginx/sites-available/crawl4ai

  1. server {
  2. listen 80;
  3. server_name crawl4ai.example.com;
  4. location / {
  5. proxy_pass http://localhost:11235;
  6. proxy_set_header Host $host;
  7. proxy_set_header X-Real-IP $remote_addr;
  8. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  9. proxy_set_header X-Forwarded-Proto $scheme;
  10. }
  11. }

启用 Nginx 配置并重启服务:

  1. sudo ln -s /etc/nginx/sites-available/crawl4ai /etc/nginx/sites-enabled/
  2. sudo systemctl restart nginx

步骤 3:使用 Docker Compose 运行 Crawl4AI

/opt/crawl4ai/ 目录中创建 docker-compose.prod.yml 文件:

  1. version: '3.8'
  2. services:
  3. crawl4ai:
  4. image: unclecode/crawl4ai:all
  5. restart: always
  6. environment:
  7. - CRAWL4AI_API_TOKEN=your_secret_token
  8. - MAX_CONCURRENT_TASKS=10
  9. ports:
  10. - "11235:11235"
  11. volumes:
  12. - /dev/shm:/dev/shm

然后运行:

  1. docker-compose -f docker-compose.prod.yml up -d

步骤 4:启用 HTTPS(Let’s Encrypt)

安装 Certbot 并获取 SSL 证书:

  1. sudo apt install certbot python3-certbot-nginx -y
  2. sudo certbot --nginx -d crawl4ai.example.com

让 HTTPS 自动续期:

  1. sudo certbot renew --dry-run

Kubernetes 部署 🛠️

如果希望在 Kubernetes(K8s) 上运行 Crawl4AI,可以使用以下 DeploymentService 资源配置。

步骤 1:创建 Kubernetes 部署文件

crawl4ai-deployment.yml 中添加以下内容:

  1. apiVersion: apps/v1
  2. kind: Deployment
  3. metadata:
  4. name: crawl4ai
  5. spec:
  6. replicas: 2
  7. selector:
  8. matchLabels:
  9. app: crawl4ai
  10. template:
  11. metadata:
  12. labels:
  13. app: crawl4ai
  14. spec:
  15. containers:
  16. - name: crawl4ai
  17. image: unclecode/crawl4ai:all
  18. env:
  19. - name: CRAWL4AI_API_TOKEN
  20. value: "your_secret_token"
  21. - name: MAX_CONCURRENT_TASKS
  22. value: "10"
  23. ports:
  24. - containerPort: 11235
  25. ---
  26. apiVersion: v1
  27. kind: Service
  28. metadata:
  29. name: crawl4ai-service
  30. spec:
  31. selector:
  32. app: crawl4ai
  33. ports:
  34. - protocol: TCP
  35. port: 80
  36. targetPort: 11235
  37. type: LoadBalancer

步骤 2:部署到 Kubernetes

  1. kubectl apply -f crawl4ai-deployment.yml

步骤 3:检查部署状态

  1. kubectl get pods
  2. kubectl get services

日志与监控 📊

查看运行日志

使用 docker logs 命令查看运行日志:

  1. docker logs -f <CONTAINER_ID>

如果使用 Docker Compose:

  1. docker-compose -f docker-compose.local.yml logs -f

如果使用 Kubernetes:

  1. kubectl logs -f deployment/crawl4ai

集成 Prometheus 监控

Crawl4AI 提供 /metrics 端点,可与 Prometheus 结合使用。

步骤 1:安装 Prometheus

  1. docker run -d -p 9090:9090 prom/prometheus

步骤 2:配置 Prometheus 采集 Crawl4AI 指标

prometheus.yml 添加:

  1. scrape_configs:
  2. - job_name: 'crawl4ai'
  3. metrics_path: '/metrics'
  4. static_configs:
  5. - targets: ['localhost:11235']

重启 Prometheus:

  1. docker restart prometheus

常见问题 ❓

1. 容器无法启动,端口被占用?

运行以下命令检查端口占用情况:

  1. sudo netstat -tulnp | grep 11235

如果端口被其他进程占用,可以更改端口号或停止占用进程:

  1. sudo kill -9 <PID>

2. 如何清理 Docker 旧版本和缓存?

  1. docker system prune -af

3. 如何重启 Crawl4AI 容器?

如果你使用的是 docker run,可以使用以下命令重启:

  1. docker restart <CONTAINER_ID>

如果使用的是 docker-compose,可以运行:

  1. docker-compose down
  2. docker-compose up -d