docker config_error ai_generated true

警告: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

其他格式: JSON · Markdown 中文 · English
95%修复率
90%置信度
1证据数
2023-11-01首次发现

版本兼容性

版本状态引入弃用备注
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.

generic

官方文档

https://docs.docker.com/compose/compose-file/04-version-and-name/

解决方案

  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' 属性的标准化配置,你可以将其重定向到新文件。

无效尝试

常见但无效的做法:

  1. 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.

  2. 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.

  3. 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.