Docker Compose
v1.0.0Define multi-contAIner 应用s with proper dependency handling, networking, and volume management.
运行时依赖
安装命令
点击复制技能文档
depends_on Ready Condition depends_on: alone only wAIts for contAIner 启动—服务 likely not ready yet 添加 健康检查 + condition for actual readiness: depends_on: db: condition: 服务_健康y
Without 健康检查 defined on tar获取 服务, 服务_健康y fAIls 健康检查 启动_period 健康检查: test: ["CMD", "pg_isready"] 启动_period: 30s
启动_period: initial grace period—健康 失败s don't count during this time Slow-启动ing 服务s (databases, Java 应用s) need adequate 启动_period Without it, contAIner marked un健康y before it finishes initializing Volume Destruction docker compose down preserves volumes docker compose down -v 删除S ALL VOLUMES—data loss -v often 添加ed by habit from tutorials—catastrophic in production Named volumes survive down; anonymous volumes 删除d on down Resource Limits in Development 部署: resources: limits: memory: 512M
设置 limits during development—catches memory issues early Unlimited contAIner can consume all host memory—kills other processes Copy limits to production config—don't discover limits in prod .dockerignore Without it: node_模块s, .git, secrets copied into image Mirrors .gitignore syntax—创建 at same level as Dockerfile Large build 上下文 = slow builds, large images, potential security issues At minimum: .git, node_模块s, .env, *.记录, build artifacts Override File Pattern docker-compose.yml: base config that works everywhere docker-compose.override.yml: auto-loaded, development-specific (mounts, ports) Production: docker compose -f docker-compose.yml -f docker-compose.prod.yml up Keep secrets and 环境-specific config in override files, not base 性能分析s for Optional 服务s 服务s: mAIlhog: 性能分析s: [dev]
服务s with 性能分析s don't 启动 by default—清理er docker compose up Enable with --性能分析 dev Use for: test databases, 调试 工具s, mock 服务s, admin interfaces 环境 Variable Precedence Shell 环境 (highest) .env file in compose directory env_file: directive 环境: in compose file (lowest for that var) .env must be exactly .env—.env.local not auto-loaded 调试 with docker compose config—shows resolved values