docker config_error ai_generated true

WARNING: The 'version' attribute in docker-compose.yml is obsolete. Use 'services' directly without 'version'.

ID: docker/compose-version-attribute-obsolete

Also available as: JSON · Markdown · 中文
95%Fix Rate
90%Confidence
1Evidence
2023-11-01First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
Docker Compose v2.20.0 active
Docker Compose v2.21.0 active
Docker Compose v2.22.0 active
Docker Desktop 4.24.0 active

Root Cause

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

中文

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

Official Documentation

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

Workarounds

  1. 95% success Remove the 'version' line from docker-compose.yml entirely. The file should start with 'services:' directly. Example: replace 'version: "3.8"\nservices:' with 'services:'.
    Remove the 'version' line from docker-compose.yml entirely. The file should start with 'services:' directly. Example: replace 'version: "3.8"\nservices:' with 'services:'.
  2. 85% success If you need to support both Compose v1 and v2, use a conditional check in your CI/CD: 'if docker compose version | grep -q v2; then sed -i '/^version:/d' docker-compose.yml; fi'.
    If you need to support both Compose v1 and v2, use a conditional check in your CI/CD: 'if docker compose version | grep -q v2; then sed -i '/^version:/d' docker-compose.yml; fi'.
  3. 90% success Use 'docker compose config' to validate and migrate your file: it will output the normalized config without the 'version' attribute, which you can redirect to a new file.
    Use 'docker compose config' to validate and migrate your file: it will output the normalized config without the 'version' attribute, which you can redirect to a new file.

中文步骤

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

Dead Ends

Common approaches that don't work:

  1. Updating the 'version' value to '3.9' or '3.8' thinking it is a version mismatch 95% fail

    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% fail

    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% fail

    Docker Compose v1 is deprecated and no longer receives security updates; downgrading introduces risks and is not a sustainable solution.