docker-compose-setup

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Docker Compose Setup Skill

Docker Compose 配置技能

Master Docker Compose for multi-container application orchestration with service dependencies, health checks, and environment management.
掌握Docker Compose的多容器应用编排,包括服务依赖、健康检查和环境管理。

Purpose

用途

Design and configure Docker Compose files for development and production environments with proper service orchestration.
为开发和生产环境设计并配置Docker Compose文件,实现合理的服务编排。

Parameters

参数

ParameterTypeRequiredDefaultDescription
servicesarrayNo-List of services to configure
environmentenumNodevdev/staging/prod
include_monitoringbooleanNofalseAdd monitoring services
参数类型是否必填默认值描述
services数组-要配置的服务列表
environment枚举dev开发/预发布/生产
include_monitoring布尔值false添加监控服务

Modern Compose File (2024-2025)

2024-2025 新版Compose文件

Note: The
version
field is deprecated. Start directly with
services:
.
yaml
services:
  frontend:
    build:
      context: ./frontend
      target: production
    ports:
      - "80:80"
    depends_on:
      backend:
        condition: service_healthy
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost/health"]
      interval: 30s
      timeout: 10s
      retries: 3
    restart: unless-stopped

  backend:
    build: ./backend
    expose:
      - "3000"
    environment:
      DATABASE_URL: postgres://user:${DB_PASSWORD}@database:5432/app
    depends_on:
      database:
        condition: service_healthy
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:3000/health"]
      interval: 10s
      timeout: 5s
      retries: 5

  database:
    image: postgres:16-alpine
    volumes:
      - db_data:/var/lib/postgresql/data
    environment:
      POSTGRES_PASSWORD: ${DB_PASSWORD}
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U postgres"]
      interval: 5s
      timeout: 5s
      retries: 5

volumes:
  db_data:
注意
version
字段已被弃用,直接以
services:
开头。
yaml
services:
  frontend:
    build:
      context: ./frontend
      target: production
    ports:
      - "80:80"
    depends_on:
      backend:
        condition: service_healthy
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost/health"]
      interval: 30s
      timeout: 10s
      retries: 3
    restart: unless-stopped

  backend:
    build: ./backend
    expose:
      - "3000"
    environment:
      DATABASE_URL: postgres://user:${DB_PASSWORD}@database:5432/app
    depends_on:
      database:
        condition: service_healthy
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:3000/health"]
      interval: 10s
      timeout: 5s
      retries: 5

  database:
    image: postgres:16-alpine
    volumes:
      - db_data:/var/lib/postgresql/data
    environment:
      POSTGRES_PASSWORD: ${DB_PASSWORD}
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U postgres"]
      interval: 5s
      timeout: 5s
      retries: 5

volumes:
  db_data:

Environment Management

环境管理

Base + Override Pattern

基础+覆盖模式

yaml
undefined
yaml
undefined

docker-compose.yaml (base)

docker-compose.yaml (base)

services: app: image: myapp:latest
services: app: image: myapp:latest

docker-compose.override.yaml (dev - auto-loaded)

docker-compose.override.yaml (dev - auto-loaded)

services: app: build: . volumes: - ./src:/app/src environment: - DEBUG=true
services: app: build: . volumes: - ./src:/app/src environment: - DEBUG=true

docker-compose.prod.yaml (production)

docker-compose.prod.yaml (production)

services: app: deploy: replicas: 3 restart: always

```bash
services: app: deploy: replicas: 3 restart: always

```bash

Development (loads override automatically)

开发环境(自动加载覆盖配置)

docker compose up
docker compose up

Production

生产环境

docker compose -f docker-compose.yaml -f docker-compose.prod.yaml up -d
undefined
docker compose -f docker-compose.yaml -f docker-compose.prod.yaml up -d
undefined

Environment Variables

环境变量

bash
undefined
bash
undefined

.env file (auto-loaded)

.env文件(自动加载)

DB_PASSWORD=secret123 APP_VERSION=1.2.3

```yaml
DB_PASSWORD=secret123 APP_VERSION=1.2.3

```yaml

Using in compose

在compose中使用

environment:
  • DB_PASSWORD=${DB_PASSWORD}
  • VERSION=${APP_VERSION:-latest} # Default value
undefined
environment:
  • DB_PASSWORD=${DB_PASSWORD}
  • VERSION=${APP_VERSION:-latest} # 默认值
undefined

Service Profiles

服务配置文件

yaml
services:
  app:
    image: myapp

  # Only with --profile debug
  debugger:
    image: debug-tools
    profiles:
      - debug

  # Only with --profile testing
  test-db:
    image: postgres:alpine
    profiles:
      - testing
bash
docker compose up                     # app only
docker compose --profile debug up     # app + debugger
yaml
services:
  app:
    image: myapp

  # 仅在--profile debug模式下启动
  debugger:
    image: debug-tools
    profiles:
      - debug

  # 仅在--profile testing模式下启动
  test-db:
    image: postgres:alpine
    profiles:
      - testing
bash
docker compose up                     # 仅启动app
docker compose --profile debug up     # 启动app + debugger

Common Commands

常用命令

bash
undefined
bash
undefined

Start services

启动服务

docker compose up -d
docker compose up -d

Rebuild and start

重新构建并启动

docker compose up -d --build
docker compose up -d --build

View logs

查看日志

docker compose logs -f backend
docker compose logs -f backend

Scale service

扩容服务

docker compose up -d --scale backend=3
docker compose up -d --scale backend=3

Stop and clean

停止并清理

docker compose down -v
docker compose down -v

Validate config

验证配置

docker compose config
undefined
docker compose config
undefined

Error Handling

错误处理

Common Errors

常见错误

ErrorCauseSolution
undefined service
Dependency missingDefine service
yaml syntax error
IndentationFix YAML
port already in use
Port conflictChange port
healthcheck failing
Service not readyIncrease start_period
错误原因解决方案
undefined service
依赖缺失定义对应的服务
yaml syntax error
缩进错误修复YAML格式
port already in use
端口冲突修改端口
healthcheck failing
服务未就绪增加start_period时长

Fallback Strategy

回退策略

  1. Validate with
    docker compose config
  2. Start services individually
  3. Use
    --no-deps
    to skip dependencies
  1. 使用
    docker compose config
    验证配置
  2. 单独启动服务
  3. 使用
    --no-deps
    跳过依赖

Troubleshooting

故障排查

Debug Checklist

调试检查清单

  • Valid YAML?
    docker compose config
  • Images available?
    docker compose pull
  • Dependencies healthy? Check healthchecks
  • Environment set? Check .env file
  • YAML格式是否有效?执行
    docker compose config
    验证
  • 镜像是否可用?执行
    docker compose pull
  • 依赖服务是否健康?检查健康状态
  • 环境变量是否设置?检查.env文件

Health Check Debugging

健康检查调试

bash
undefined
bash
undefined

Check health status

检查健康状态

docker inspect --format='{{json .State.Health}}' <container>
docker inspect --format='{{json .State.Health}}' <container>

View health logs

查看健康检查日志

docker inspect --format='{{range .State.Health.Log}}{{.Output}}{{end}}' <container>
undefined
docker inspect --format='{{range .State.Health.Log}}{{.Output}}{{end}}' <container>
undefined

Usage

使用方法

Skill("docker-compose-setup")
Skill("docker-compose-setup")

Related Skills

相关技能

  • docker-networking
  • docker-volumes
  • docker-production
  • docker-networking
  • docker-volumes
  • docker-production