version: '3.8' services: # PostgreSQL 数据库(使用最新版 17) postgres: image: m.daocloud.io/docker.io/pgvector/pgvector:pg17 container_name: life-echo-postgres ports: - "127.0.0.1:5432:5432" # 仅绑定 localhost,通过 SSH 隧道访问 environment: POSTGRES_USER: ${POSTGRES_USER:-postgres} POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-postgres} POSTGRES_DB: ${POSTGRES_DB:-life_echo} volumes: - postgres_data:/var/lib/postgresql/data restart: always healthcheck: test: ["CMD-SHELL", "pg_isready -U postgres"] interval: 10s timeout: 5s retries: 5 networks: - life-echo-network logging: driver: "json-file" options: max-size: "10m" max-file: "3" # Redis 服务(用于会话存储和 Celery 消息队列) redis: image: m.daocloud.io/docker.io/library/redis:7-alpine container_name: life-echo-redis # ports: # - "6379:6379" # 不暴露到宿主机,仅在 Docker 网络内部访问 volumes: - redis_data:/data command: redis-server --appendonly yes --maxmemory 256mb --maxmemory-policy allkeys-lru restart: always healthcheck: test: ["CMD", "redis-cli", "ping"] interval: 10s timeout: 5s retries: 5 networks: - life-echo-network logging: driver: "json-file" options: max-size: "10m" max-file: "3" # FastAPI 应用 # 运行时统一读取 .env;部署时在远端将 .env.staging 或 .env.production 复制为 .env。 api: build: context: . dockerfile: Dockerfile image: life-echo-api:latest container_name: life-echo-api-prod expose: - "8000" env_file: - .env environment: - ASR_MODEL_CACHE_DIR=/app/models/whisper volumes: - /root/apiclient_key.pem:/app/certs/apiclient_key.pem:ro restart: always depends_on: postgres: condition: service_healthy redis: condition: service_healthy healthcheck: test: ["CMD", "python", "-c", "import http.client; conn = http.client.HTTPConnection('localhost', 8000); conn.request('GET', '/health'); r = conn.getresponse(); exit(0 if r.status == 200 else 1)"] interval: 30s timeout: 10s retries: 3 start_period: 15s networks: - life-echo-network logging: driver: "json-file" options: max-size: "10m" max-file: "3" # Celery Worker(后台任务处理,禁用镜像内置的 8000 端口健康检查,改用 celery inspect ping) celery-worker: build: context: . dockerfile: Dockerfile image: life-echo-api:latest container_name: life-echo-celery-worker command: uv run celery -A app.tasks.celery_app worker --loglevel=info --concurrency=4 env_file: - .env restart: always depends_on: postgres: condition: service_healthy redis: condition: service_healthy api: condition: service_healthy healthcheck: test: ["CMD-SHELL", "uv run celery -A app.tasks.celery_app inspect ping --timeout 10 2>/dev/null | grep -q pong || exit 1"] interval: 30s timeout: 15s retries: 3 start_period: 30s networks: - life-echo-network logging: driver: "json-file" options: max-size: "10m" max-file: "3" caddy: image: m.daocloud.io/docker.io/library/caddy:2-alpine container_name: life-echo-caddy depends_on: api: condition: service_healthy ports: - "80:80" - "443:443" volumes: - ./Caddyfile:/etc/caddy/Caddyfile:ro - caddy_data:/data - caddy_config:/config restart: always networks: - life-echo-network logging: driver: "json-file" options: max-size: "10m" max-file: "3" # Celery Beat(定时任务调度,可选) # celery-beat: # build: # context: . # dockerfile: Dockerfile # image: life-echo-api:latest # container_name: life-echo-celery-beat # command: celery -A app.tasks.celery_app beat --loglevel=info # env_file: # - .env # environment: # - DATABASE_URL=postgresql://postgres:postgres@postgres:5432/life_echo # - REDIS_URL=redis://redis:6379/0 # restart: always # depends_on: # postgres: # condition: service_healthy # redis: # condition: service_healthy # networks: # - life-echo-network # Flower(Celery 监控面板,可选) # flower: # build: # context: . # dockerfile: Dockerfile # image: life-echo-api:latest # container_name: life-echo-flower # command: celery -A app.tasks.celery_app flower --port=5555 # ports: # - "5555:5555" # env_file: # - .env # environment: # - REDIS_URL=redis://redis:6379/0 # restart: always # depends_on: # redis: # condition: service_healthy # networks: # - life-echo-network networks: life-echo-network: external: true name: api_life-echo-network volumes: postgres_data: driver: local redis_data: driver: local caddy_data: driver: local caddy_config: driver: local