警告:docker-compose.yml 中的 'version' 属性已过时。请直接使用 'services' 而不使用 'version'。
WARNING: The 'version' attribute in docker-compose.yml is obsolete. Use 'services' directly without 'version'.
ID: docker/compose-version-attribute-obsolete
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| Docker Compose v2.20.0 | active | — | — | — |
| Docker Compose v2.21.0 | active | — | — | — |
| Docker Compose v2.22.0 | active | — | — | — |
| Docker Desktop 4.24.0 | active | — | — | — |
根因分析
Docker Compose v2.20+ 不再支持顶级 'version' 键;该键用于旧版 Compose v1 文件,现在被忽略或导致警告。
English
Docker Compose v2.20+ no longer supports the 'version' top-level key; it was used in legacy Compose v1 files and is now ignored or causes a warning.
官方文档
https://docs.docker.com/compose/compose-file/04-version-and-name/解决方案
-
完全从 docker-compose.yml 中删除 'version' 行。文件应直接以 'services:' 开头。例如:将 'version: "3.8"\nservices:' 替换为 'services:'。
-
如果需要同时支持 Compose v1 和 v2,在 CI/CD 中使用条件检查:'if docker compose version | grep -q v2; then sed -i '/^version:/d' docker-compose.yml; fi'。
-
使用 'docker compose config' 验证并迁移文件:它会输出没有 'version' 属性的标准化配置,你可以将其重定向到新文件。
无效尝试
常见但无效的做法:
-
Updating the 'version' value to '3.9' or '3.8' thinking it is a version mismatch
95% 失败
The warning is not about the version number being wrong; the entire attribute is deprecated. Changing the value does not remove the warning.
-
Ignoring the warning because 'docker compose up' still works
40% 失败
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.
-
Downgrading Docker Compose to v1 to keep using the 'version' attribute
70% 失败
Docker Compose v1 is deprecated and no longer receives security updates; downgrading introduces risks and is not a sustainable solution.