# 本地开发环境:docker compose up -d --build # 镜像版本仅作示例,按项目需要调整 services: api: build: . ports: - "8080:8080" environment: DATABASE_URL: postgres://app:app@db:5432/app?sslmode=disable # 服务名 db 就是主机名 REDIS_URL: redis://redis:6379/0 depends_on: db: condition: service_healthy # 等数据库健康检查通过再启动 redis: condition: service_started restart: unless-stopped db: image: postgres:17 environment: POSTGRES_USER: app POSTGRES_PASSWORD: app # 仅限本地开发;生产用密钥管理 POSTGRES_DB: app volumes: - pgdata:/var/lib/postgresql/data # 命名卷:容器删除后数据仍在 healthcheck: test: ["CMD-SHELL", "pg_isready -U app -d app"] interval: 5s timeout: 3s retries: 10 redis: image: redis:7-alpine volumes: pgdata: