compose
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseDocker Compose Configuration Skill
Docker Compose 配置技能
Overview
概述
This skill generates Docker Compose configurations for multi-container applications. It creates properly structured compose files with:
- Service definitions
- Network configuration
- Volume management
- Health checks
- Dependencies
- Environment configuration
本技能用于为多容器应用生成Docker Compose配置,会创建结构规范的compose文件,包含以下内容:
- 服务定义
- 网络配置
- 存储卷管理
- 健康检查
- 依赖关系
- 环境配置
Activation
适用场景
Use this skill when:
- Creating a new compose file
- Adding services to existing compose
- Configuring container orchestration
- Setting up development or production environments
在以下场景使用本技能:
- 创建新的compose文件
- 向现有compose配置中添加服务
- 配置容器编排
- 搭建开发或生产环境
Process
执行流程
1. Understand Requirements
1. 明确需求
Gather information about:
- Required services (web, api, database, cache, etc.)
- Environment (development/production)
- Networking needs (internal, external, SSL)
- Persistence requirements
- Scaling needs
收集以下相关信息:
- 所需服务(web、api、数据库、缓存等)
- 运行环境(开发/生产)
- 网络需求(内部、外部、SSL)
- 持久化要求
- 扩缩容需求
2. Consult Documentation
2. 查阅参考文档
Read relevant documentation:
- for structure
03-compose-fundamentals.md - for networks
04-networking.md - for database services
05-databases.md - for dependencies
06-services.md - for persistence
08-volumes.md
阅读相关文档:
- 结构规范参考
03-compose-fundamentals.md - 网络配置参考
04-networking.md - 数据库服务配置参考
05-databases.md - 依赖配置参考
06-services.md - 持久化配置参考
08-volumes.md
3. Generate Configuration
3. 生成配置
Create compose file following best practices:
yaml
undefined遵循最佳实践创建compose文件:
yaml
undefinedModern compose (no version field)
Modern compose (no version field)
services:
app:
build:
context: .
dockerfile: Dockerfile
restart: unless-stopped
ports:
- "3000:3000"
environment:
- NODE_ENV=production
depends_on:
db:
condition: service_healthy
networks:
- frontend
- backend
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:3000/health"]
interval: 30s
timeout: 10s
retries: 3
db:
image: postgres:16-alpine
restart: unless-stopped
environment:
POSTGRES_USER: ${DB_USER}
POSTGRES_PASSWORD: ${DB_PASSWORD}
POSTGRES_DB: ${DB_NAME}
volumes:
- postgres_data:/var/lib/postgresql/data
networks:
- backend
healthcheck:
test: ["CMD-SHELL", "pg_isready -U ${DB_USER}"]
interval: 10s
timeout: 5s
retries: 5
volumes:
postgres_data:
networks:
frontend:
backend:
internal: true
undefinedservices:
app:
build:
context: .
dockerfile: Dockerfile
restart: unless-stopped
ports:
- "3000:3000"
environment:
- NODE_ENV=production
depends_on:
db:
condition: service_healthy
networks:
- frontend
- backend
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:3000/health"]
interval: 30s
timeout: 10s
retries: 3
db:
image: postgres:16-alpine
restart: unless-stopped
environment:
POSTGRES_USER: ${DB_USER}
POSTGRES_PASSWORD: ${DB_PASSWORD}
POSTGRES_DB: ${DB_NAME}
volumes:
- postgres_data:/var/lib/postgresql/data
networks:
- backend
healthcheck:
test: ["CMD-SHELL", "pg_isready -U ${DB_USER}"]
interval: 10s
timeout: 5s
retries: 5
volumes:
postgres_data:
networks:
frontend:
backend:
internal: true
undefinedService Patterns
服务配置模式
Web Application
Web应用
yaml
services:
web:
build: .
ports:
- "80:80"
depends_on:
- apiyaml
services:
web:
build: .
ports:
- "80:80"
depends_on:
- apiAPI Service
API服务
yaml
services:
api:
build: ./api
expose:
- "3000"
environment:
- DATABASE_URL=postgresql://user:pass@db:5432/app
depends_on:
db:
condition: service_healthyyaml
services:
api:
build: ./api
expose:
- "3000"
environment:
- DATABASE_URL=postgresql://user:pass@db:5432/app
depends_on:
db:
condition: service_healthyDatabase Service
数据库服务
yaml
services:
db:
image: postgres:16-alpine
volumes:
- db_data:/var/lib/postgresql/data
environment:
POSTGRES_PASSWORD: ${DB_PASSWORD}
healthcheck:
test: ["CMD-SHELL", "pg_isready"]
interval: 10s
retries: 5yaml
services:
db:
image: postgres:16-alpine
volumes:
- db_data:/var/lib/postgresql/data
environment:
POSTGRES_PASSWORD: ${DB_PASSWORD}
healthcheck:
test: ["CMD-SHELL", "pg_isready"]
interval: 10s
retries: 5Cache Service
缓存服务
yaml
services:
redis:
image: redis:7-alpine
command: redis-server --appendonly yes
volumes:
- redis_data:/datayaml
services:
redis:
image: redis:7-alpine
command: redis-server --appendonly yes
volumes:
- redis_data:/dataBackground Worker
后台工作进程
yaml
services:
worker:
build: .
command: npm run worker
depends_on:
- db
- redisyaml
services:
worker:
build: .
command: npm run worker
depends_on:
- db
- redisNetwork Patterns
网络配置模式
Internal Database Network
内部数据库网络
yaml
networks:
backend:
internal: true # No external accessyaml
networks:
backend:
internal: true # No external accessExternal Shared Network
外部共享网络
yaml
networks:
proxy:
external: true
name: traefik_proxyyaml
networks:
proxy:
external: true
name: traefik_proxyVolume Patterns
存储卷配置模式
Named Volume
命名存储卷
yaml
volumes:
postgres_data:yaml
volumes:
postgres_data:Bind Mount (Development)
绑定挂载(开发环境)
yaml
volumes:
- ./src:/app/srcyaml
volumes:
- ./src:/app/srcAnonymous Volume
匿名存储卷
yaml
volumes:
- /app/node_modulesyaml
volumes:
- /app/node_modulesEnvironment Patterns
环境变量配置模式
Direct Values
直接赋值
yaml
environment:
- NODE_ENV=productionyaml
environment:
- NODE_ENV=productionFrom .env File
从.env文件加载
yaml
env_file:
- .envyaml
env_file:
- .envWith Defaults
带默认值
yaml
environment:
- DB_HOST=${DB_HOST:-localhost}yaml
environment:
- DB_HOST=${DB_HOST:-localhost}Output
输出内容
Generated compose file includes:
- All required services
- Proper network configuration
- Volume definitions
- Health checks
- Dependency management
- Environment configuration
- Comments for complex settings
生成的compose文件包含:
- 所有所需服务
- 规范的网络配置
- 存储卷定义
- 健康检查配置
- 依赖管理配置
- 环境变量配置
- 复杂配置项的注释说明