docker-compose-setup
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseDocker 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
参数
| Parameter | Type | Required | Default | Description |
|---|---|---|---|---|
| services | array | No | - | List of services to configure |
| environment | enum | No | dev | dev/staging/prod |
| include_monitoring | boolean | No | false | Add monitoring services |
| 参数 | 类型 | 是否必填 | 默认值 | 描述 |
|---|---|---|---|---|
| services | 数组 | 否 | - | 要配置的服务列表 |
| environment | 枚举 | 否 | dev | 开发/预发布/生产 |
| include_monitoring | 布尔值 | 否 | false | 添加监控服务 |
Modern Compose File (2024-2025)
2024-2025 新版Compose文件
Note: The field is deprecated. Start directly with .
versionservices: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:注意:字段已被弃用,直接以开头。
versionservices: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
undefinedyaml
undefineddocker-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
```bashservices:
app:
deploy:
replicas: 3
restart: always
```bashDevelopment (loads override automatically)
开发环境(自动加载覆盖配置)
docker compose up
docker compose up
Production
生产环境
docker compose -f docker-compose.yaml -f docker-compose.prod.yaml up -d
undefineddocker compose -f docker-compose.yaml -f docker-compose.prod.yaml up -d
undefinedEnvironment Variables
环境变量
bash
undefinedbash
undefined.env file (auto-loaded)
.env文件(自动加载)
DB_PASSWORD=secret123
APP_VERSION=1.2.3
```yamlDB_PASSWORD=secret123
APP_VERSION=1.2.3
```yamlUsing in compose
在compose中使用
environment:
- DB_PASSWORD=${DB_PASSWORD}
- VERSION=${APP_VERSION:-latest} # Default value
undefinedenvironment:
- DB_PASSWORD=${DB_PASSWORD}
- VERSION=${APP_VERSION:-latest} # 默认值
undefinedService 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:
- testingbash
docker compose up # app only
docker compose --profile debug up # app + debuggeryaml
services:
app:
image: myapp
# 仅在--profile debug模式下启动
debugger:
image: debug-tools
profiles:
- debug
# 仅在--profile testing模式下启动
test-db:
image: postgres:alpine
profiles:
- testingbash
docker compose up # 仅启动app
docker compose --profile debug up # 启动app + debuggerCommon Commands
常用命令
bash
undefinedbash
undefinedStart 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
undefineddocker compose config
undefinedError Handling
错误处理
Common Errors
常见错误
| Error | Cause | Solution |
|---|---|---|
| Dependency missing | Define service |
| Indentation | Fix YAML |
| Port conflict | Change port |
| Service not ready | Increase start_period |
| 错误 | 原因 | 解决方案 |
|---|---|---|
| 依赖缺失 | 定义对应的服务 |
| 缩进错误 | 修复YAML格式 |
| 端口冲突 | 修改端口 |
| 服务未就绪 | 增加start_period时长 |
Fallback Strategy
回退策略
- Validate with
docker compose config - Start services individually
- Use to skip dependencies
--no-deps
- 使用验证配置
docker compose config - 单独启动服务
- 使用跳过依赖
--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
undefinedbash
undefinedCheck 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>
undefineddocker inspect --format='{{range .State.Health.Log}}{{.Output}}{{end}}' <container>
undefinedUsage
使用方法
Skill("docker-compose-setup")Skill("docker-compose-setup")Related Skills
相关技能
- docker-networking
- docker-volumes
- docker-production
- docker-networking
- docker-volumes
- docker-production