From 84a801f3acb0d9e7e1534bfbc5d8997aa5428132 Mon Sep 17 00:00:00 2001 From: yeasy Date: Sat, 25 Apr 2026 21:03:39 +0000 Subject: [PATCH] Document depends_on condition and healthcheck --- 11_compose/11.5_compose_file.md | 38 ++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/11_compose/11.5_compose_file.md b/11_compose/11.5_compose_file.md index ad424db..1076fc9 100644 --- a/11_compose/11.5_compose_file.md +++ b/11_compose/11.5_compose_file.md @@ -132,7 +132,43 @@ services: db: image: postgres ``` -> 注意:`web` 服务不会等待 `redis` `db` “完全启动” 之后才启动。 + +> 注意:上述简写形式中,`web` 服务不会等待 `redis` `db` “完全启动” 之后才启动。 + +如果需要等待依赖服务就绪,可以使用 `condition` 字段配合 `healthcheck`: + +```yaml +services: + web: + build: . + depends_on: + db: + condition: service_healthy + redis: + condition: service_healthy + + redis: + image: redis + healthcheck: + test: [“CMD”, “redis-cli”, “ping”] + interval: 5s + timeout: 3s + retries: 5 + + db: + image: postgres + healthcheck: + test: [“CMD-SHELL”, “pg_isready -U postgres”] + interval: 5s + timeout: 3s + retries: 5 +``` + +`condition` 支持三个值: + +* `service_started`:容器启动即满足(默认)。 +* `service_healthy`:容器的 `healthcheck` 状态为 healthy 时满足。 +* `service_completed_successfully`:容器成功退出(退出码为 0)时满足,适用于初始化任务等一次性容器。 ### 11.5.10 `dns`