Files
life-echo/api/docker-compose.yml

183 lines
5.1 KiB
YAML
Raw Normal View History

services:
# PostgreSQL 数据库(使用最新版 17
postgres:
image: m.daocloud.io/docker.io/pgvector/pgvector:pg17
container_name: life-echo-postgres
ports:
- "127.0.0.1:${POSTGRES_HOST_PORT:-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 应用
2026-03-23 13:21:07 +08:00
# 运行时统一读取 .env部署时在远端将 .env.staging 或 .env.production 复制为 .env。
api:
build:
context: .
dockerfile: Dockerfile
image: life-echo-api:latest
container_name: life-echo-api-prod
2026-05-09 16:16:48 +08:00
# 默认仅绑定本机回环,交给宿主机 Caddy/反代staging 如需 IP:port 直连,可在 .env 设置 LIFE_ECHO_API_HOST_BIND=0.0.0.0。
# 若与 Cosmetic 等共用主机且 8000 已被占用,在 .env 中设置 LIFE_ECHO_API_HOST_PORT=其它端口并在 Caddyfile / app env 中一致。
2026-03-25 10:26:21 +08:00
ports:
2026-05-09 16:16:48 +08:00
- "${LIFE_ECHO_API_HOST_BIND:-127.0.0.1}:${LIFE_ECHO_API_HOST_PORT:-8000}:8000"
env_file:
2026-03-23 13:21:07 +08:00
- .env
environment:
- ASR_MODEL_CACHE_DIR=/app/models/whisper
- ALEMBIC_STARTUP_FAIL_FAST=true
- APP_ENV=production
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: 15s
timeout: 5s
retries: 6
start_period: 120s
networks:
- life-echo-network
logging:
driver: "json-file"
options:
max-size: "10m"
max-file: "3"
2026-02-11 18:34:22 +01:00
# 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 -Q celery,memory_idle
env_file:
2026-03-23 13:21:07 +08:00
- .env
environment:
- APP_ENV=production
restart: always
depends_on:
postgres:
condition: service_healthy
redis:
condition: service_healthy
api:
condition: service_healthy
2026-02-11 18:34:22 +01:00
healthcheck:
2026-03-23 10:25:51 +08:00
test: ["CMD-SHELL", "uv run celery -A app.tasks.celery_app inspect ping --timeout 10 2>/dev/null | grep -q pong || exit 1"]
2026-02-11 18:34:22 +01:00
interval: 30s
timeout: 15s
retries: 3
start_period: 30s
networks:
- life-echo-network
logging:
driver: "json-file"
options:
max-size: "10m"
max-file: "3"
celery-beat:
build:
context: .
dockerfile: Dockerfile
image: life-echo-api:latest
container_name: life-echo-celery-beat
command: uv run celery -A app.tasks.celery_app beat --loglevel=info
env_file:
- .env
environment:
- APP_ENV=production
restart: always
depends_on:
postgres:
condition: service_healthy
redis:
condition: service_healthy
celery-worker:
condition: service_started
networks:
- life-echo-network
logging:
driver: "json-file"
options:
max-size: "10m"
max-file: "3"
# FlowerCelery 监控面板,可选)
# 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:
2026-03-23 13:21:07 +08:00
# - .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