60 lines
1.8 KiB
YAML
60 lines
1.8 KiB
YAML
services:
|
|
postgres:
|
|
image: postgres:16-alpine
|
|
environment:
|
|
POSTGRES_USER: ${POSTGRES_USER:?POSTGRES_USER 未设置}
|
|
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:?POSTGRES_PASSWORD 未设置}
|
|
POSTGRES_DB: ${POSTGRES_DB:-football}
|
|
ports:
|
|
- "${POSTGRES_PORT:-5433}:5432"
|
|
volumes:
|
|
- pgdata:/var/lib/postgresql/data
|
|
healthcheck:
|
|
test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER:?POSTGRES_USER 未设置}"]
|
|
interval: 5s
|
|
timeout: 5s
|
|
retries: 5
|
|
|
|
api:
|
|
build: .
|
|
# Fix 2: 容器内 DATABASE_URL 使用 postgres 服务名(非 localhost)
|
|
# Fix 3: 启动时先跑 alembic upgrade,再启 uvicorn
|
|
command: >
|
|
sh -c "alembic upgrade head &&
|
|
uvicorn src.api.app:app --host 0.0.0.0 --port 8000"
|
|
ports:
|
|
- "${API_PORT:-8000}:8000"
|
|
environment:
|
|
# Fix 2: 容器内 DATABASE_URL 使用 postgres 服务名(非 localhost)
|
|
# 必须覆盖 .env 中的 DATABASE_URL,因为 Settings 不读 DB_HOST/DB_PORT
|
|
DATABASE_URL: postgresql+asyncpg://${POSTGRES_USER:?POSTGRES_USER 未设置}:${POSTGRES_PASSWORD:?POSTGRES_PASSWORD 未设置}@postgres:5432/${POSTGRES_DB:-football}
|
|
env_file: .env
|
|
healthcheck:
|
|
test: ["CMD-SHELL", "curl -sf http://localhost:8000/health/ready || exit 1"]
|
|
interval: 10s
|
|
timeout: 5s
|
|
retries: 3
|
|
start_period: 10s
|
|
depends_on:
|
|
postgres:
|
|
condition: service_healthy
|
|
volumes:
|
|
- ./src:/app/src
|
|
- ./alembic:/app/alembic
|
|
- ./alembic.ini:/app/alembic.ini
|
|
|
|
frontend:
|
|
# Fix 4: 多阶段构建 —— 先 build 静态文件,再复制到 nginx
|
|
build:
|
|
context: ./frontend
|
|
dockerfile: Dockerfile.frontend
|
|
ports:
|
|
- "${FRONTEND_PORT:-3000}:80"
|
|
volumes:
|
|
- ./frontend/nginx.conf:/etc/nginx/conf.d/default.conf:ro
|
|
depends_on:
|
|
- api
|
|
|
|
volumes:
|
|
pgdata:
|