OpenClaw Docker 部署:容器化最佳实践

0 阅读1分钟

一次配置,到处运行

为什么用 Docker?

  • 📦 环境一致:开发、测试、生产完全一样
  • 🚀 快速部署:一条命令启动
  • 🔧 易于维护:升级、回滚简单
  • 🌐 跨平台:Linux、macOS、Windows

快速开始

方式 1:Docker 命令行

# 拉取镜像
docker pull openclaw/openclaw:latest

# 运行
docker run -d \
  --name openclaw \
  -e DEEPSEEK_API_KEY=sk-xxx \
  -v ~/.openclaw:/root/.openclaw \
  -p 3000:3000 \
  openclaw/openclaw:latest

方式 2:Docker Compose(推荐)

# docker-compose.yml
version: '3.8'

services:
  openclaw:
    image: openclaw/openclaw:latest
    container_name: openclaw
    restart: unless-stopped
    environment:
      - DEEPSEEK_API_KEY=${DEEPSEEK_API_KEY}
      - TELEGRAM_BOT_TOKEN=${TELEGRAM_BOT_TOKEN}
    volumes:
      - ./config:/root/.openclaw
      - ./data:/root/.openclaw/data
      - ./logs:/root/.openclaw/logs
    ports:
      - "3000:3000"
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:3000/health"]
      interval: 30s
      timeout: 10s
      retries: 3
# 启动
docker-compose up -d

# 查看日志
docker-compose logs -f

# 停止
docker-compose down

完整配置

带数据库

version: '3.8'

services:
  openclaw:
    image: openclaw/openclaw:latest
    depends_on:
      - postgres
      - redis
    environment:
      - DATABASE_URL=postgresql://user:pass@postgres:5432/openclaw
      - REDIS_URL=redis://redis:6379
    ports:
      - "3000:3000"

  postgres:
    image: postgres:16
    environment:
      - POSTGRES_DB=openclaw
      - POSTGRES_USER=user
      - POSTGRES_PASSWORD=pass
    volumes:
      - postgres-data:/var/lib/postgresql/data

  redis:
    image: redis:7
    volumes:
      - redis-data:/data

volumes:
  postgres-data:
  redis-data:

带 Nginx

version: '3.8'

services:
  openclaw:
    image: openclaw/openclaw:latest
    expose:
      - "3000"

  nginx:
    image: nginx:alpine
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ./nginx.conf:/etc/nginx/nginx.conf
      - ./ssl:/etc/nginx/ssl
    depends_on:
      - openclaw

生产环境配置

1. 环境变量

# .env
DEEPSEEK_API_KEY=sk-xxx
TELEGRAM_BOT_TOKEN=123456:ABC
DATABASE_URL=postgresql://...
# docker-compose.yml
services:
  openclaw:
    env_file: .env

2. 资源限制

services:
  openclaw:
    deploy:
      resources:
        limits:
          cpus: '2'
          memory: 4G
        reservations:
          cpus: '1'
          memory: 2G

3. 日志配置

services:
  openclaw:
    logging:
      driver: "json-file"
      options:
        max-size: "10m"
        max-file: "3"

4. 自动重启

services:
  openclaw:
    restart: unless-stopped

升级策略

# 拉取新镜像
docker pull openclaw/openclaw:latest

# 停止旧容器
docker-compose down

# 启动新容器
docker-compose up -d

备份与恢复

备份

# 备份数据
docker run --rm \
  -v openclaw-data:/data \
  -v $(pwd):/backup \
  alpine tar czf /backup/backup.tar.gz /data

恢复

# 恢复数据
docker run --rm \
  -v openclaw-data:/data \
  -v $(pwd):/backup \
  alpine tar xzf /backup/backup.tar.gz -C /

多实例部署

version: '3.8'

services:
  openclaw-1:
    image: openclaw/openclaw:latest
    ports:
      - "3001:3000"

  openclaw-2:
    image: openclaw/openclaw:latest
    ports:
      - "3002:3000"

  nginx:
    image: nginx:alpine
    ports:
      - "80:80"
    # 负载均衡配置

Kubernetes 部署

# deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: openclaw
spec:
  replicas: 3
  selector:
    matchLabels:
      app: openclaw
  template:
    metadata:
      labels:
        app: openclaw
    spec:
      containers:
      - name: openclaw
        image: openclaw/openclaw:latest
        env:
        - name: DEEPSEEK_API_KEY
          valueFrom:
            secretKeyRef:
              name: openclaw-secrets
              key: api-key
        resources:
          requests:
            memory: "512Mi"
            cpu: "500m"
          limits:
            memory: "2Gi"
            cpu: "1000m"
        livenessProbe:
          httpGet:
            path: /health
            port: 3000
          initialDelaySeconds: 30
          periodSeconds: 10
        readinessProbe:
          httpGet:
            path: /health
            port: 3000
          initialDelaySeconds: 5
          periodSeconds: 5
# service.yaml
apiVersion: v1
kind: Service
metadata:
  name: openclaw
spec:
  selector:
    app: openclaw
  ports:
  - port: 80
    targetPort: 3000
  type: LoadBalancer

故障排查

# 查看容器状态
docker ps -a

# 查看日志
docker logs openclaw

# 进入容器
docker exec -it openclaw sh

# 检查健康
docker inspect openclaw --format='{{.State.Health.Status}}'

最佳实践

  1. 使用 .env 文件:敏感信息不要写死
  2. 资源限制:防止容器占用过多
  3. 健康检查:自动重启异常容器
  4. 日志管理:限制日志大小,防止占满磁盘
  5. 定期备份:重要数据定期备份

💬 你用 Docker 部署什么?评论区分享!

🎯 需要 Docker 部署服务?微信:yang1002378395