# 警告：docker-compose.yml 中的 'version' 属性已过时。请直接使用 'services' 而不使用 'version'。

- **ID:** `docker/compose-version-attribute-obsolete`
- **领域:** docker
- **类别:** config_error
- **验证级别:** ai_generated
- **修复率:** 95%

## 根因

Docker Compose v2.20+ 不再支持顶级 'version' 键；该键用于旧版 Compose v1 文件，现在被忽略或导致警告。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| Docker Compose v2.20.0 | active | — | — |
| Docker Compose v2.21.0 | active | — | — |
| Docker Compose v2.22.0 | active | — | — |
| Docker Desktop 4.24.0 | active | — | — |

## 解决方案

1. ```
   完全从 docker-compose.yml 中删除 'version' 行。文件应直接以 'services:' 开头。例如：将 'version: "3.8"\nservices:' 替换为 'services:'。
   ```
2. ```
   如果需要同时支持 Compose v1 和 v2，在 CI/CD 中使用条件检查：'if docker compose version | grep -q v2; then sed -i '/^version:/d' docker-compose.yml; fi'。
   ```
3. ```
   使用 'docker compose config' 验证并迁移文件：它会输出没有 'version' 属性的标准化配置，你可以将其重定向到新文件。
   ```

## 无效尝试

- **Updating the 'version' value to '3.9' or '3.8' thinking it is a version mismatch** — The warning is not about the version number being wrong; the entire attribute is deprecated. Changing the value does not remove the warning. (95% 失败率)
- **Ignoring the warning because 'docker compose up' still works** — While the warning does not prevent execution, future versions of Docker Compose may treat the presence of 'version' as a fatal error, breaking the setup. (40% 失败率)
- **Downgrading Docker Compose to v1 to keep using the 'version' attribute** — Docker Compose v1 is deprecated and no longer receives security updates; downgrading introduces risks and is not a sustainable solution. (70% 失败率)
