模板(Compose v3.8 语法)version: "3.8" services: web: image: nginx:1.25 ports: - "80:80" restart: always depends_on: - app healthcheck: test: ["CMD", "curl", "-f", "http://localhost"] interval: 30s timeout: 5s retries: 3 logging: driver: "json-file" options: max-size: "10m" max-file: "3" app: build: ./app env_file: - ./.env environment: - NODE_ENV=production volumes: - app-data:/data restart: unless-stopped healthcheck: test: ["CMD", "node", "healthcheck.js"] interval: 30s timeout: 5s retries: 3 volumes: app-data: 关键参数说明(稳定且常用)`restart`: `always`、`unless-stopped` 在非 Swarm 模式可用。`healthcheck`: 支持 `CMD` 数组形式;间隔与重试参数有效。`logging.json-file`: `max-size` 与 `max-file` 控制日志滚动。`env_file` 与 `environment`: 管理环境变量与敏感信息。注意:`deploy` 字段仅在 Swarm 模式生效,普通 Compose 不使用。验证`docker compose config` 检查语法与展开后的配置。`docker compose up -d` 启动后使用 `docker compose ps` 与 `docker inspect` 查看健康检查状态。

发表评论 取消回复