返回
创建于
状态公开
docker compose up --wait 是 Docker Compose v2.20+ 新增的一项功能,用来:
✅ 等到所有服务变为 “健康 (healthy)” 状态后才退出或继续执行
也就是说,使用 --wait 时:
- Compose 会启动服务
- 监控每个 service 的
healthcheck - 全部变为
healthy才会继续(exit code = 0) - 如果有任何一个变成
unhealthy或启动失败会报错(exit code ≠ 0)
🔧 使用示例
1. 基本用法
1docker compose up --wait2. 常用于 CI/CD 或脚本
确保服务准备完毕再执行后续命令:
1docker compose up -d --wait
2./run-tests.sh🔍 条件:服务必须配置 healthcheck
否则 --wait 没有意义,因为没有健康状态可等。
示例:
1services:
2 db:
3 image: mysql
4 healthcheck:
5 test: ["CMD", "mysqladmin", "ping", "-h", "localhost"]
6 interval: 5s
7 timeout: 3s
8 retries: 5🎯 会输出状态
成功:
1Service db is healthy
2Service api is healthy如果失败:
1Service api failed to reach healthy status💡 提示
如果你加上 --wait 却发现它很快退出或没有等待,多半是因为你的服务 没有 healthcheck。