# 错误：检测到循环依赖：service1 依赖于 service2，而 service2 又依赖于 service1

- **ID:** `docker/compose-service-dependency-cycle`
- **领域:** docker
- **类别:** config_error
- **验证级别:** ai_generated
- **修复率:** 95%

## 根因

在 docker-compose.yml 文件中，服务通过 'depends_on' 指令形成了循环依赖链，Docker Compose 无法解析。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| Docker Compose v2.0.0 | active | — | — |
| Docker Compose v2.20.0 | active | — | — |
| Docker Compose v2.24.0 | active | — | — |

## 解决方案

1. ```
   Redesign the service architecture to remove the circular dependency. For example, if service A needs service B and vice versa, consider merging them into a single service or using a message queue. Update docker-compose.yml to remove the cycle.
   ```
2. ```
   Use 'depends_on' with 'condition: service_healthy' and implement health checks instead of direct dependencies. This allows services to start in any order but wait for readiness. Example: 'depends_on: { db: { condition: service_healthy } }'.
   ```

## 无效尝试

- **** — Removing all depends_on directives breaks the intended startup order and may cause runtime failures if services depend on each other for readiness. (70% 失败率)
- **** — Adding a third service that depends on both only masks the cycle temporarily; the underlying dependency graph remains invalid. (80% 失败率)
