docker-撰写:撰写文件“...”无效,因为:不支持的配置选项

docker-compose: Compose file "..." is invalid because: unsupported config options

我最近从 docker 中提取了一个 official repository,其中包含 docker-compose 模板。

当我尝试构建 运行 nginx-golang-mysql/docker-compose.ymldocker-compose up -d 我得到以下错误:

ERROR: The Compose file './docker-compose.yaml' is invalid because:
Unsupported config option for volumes: 'db-data'
Unsupported config option for secrets: 'db-password'
Unsupported config option for services: 'proxy'

这是我的 docker-compose.yml 文件 (original available on github):

services:
  backend:
    build: backend
    secrets:
      - db-password
    depends_on:
      db:
        condition: service_healthy
  db:
    # We use a mariadb image which supports both amd64 & arm64 architecture
    image: mariadb:10.6.4-focal
    # If you really want to use MySQL, uncomment the following line
    #image: mysql:8.0.27
    command: '--default-authentication-plugin=mysql_native_password'
    restart: always
    healthcheck:
      test: ["CMD", "mysqladmin", "ping", "-h", "127.0.0.1", "--silent"]
      interval: 3s
      retries: 5
      start_period: 30s
    secrets:
      - db-password
    volumes:
      - db-data:/var/lib/mysql
    environment:
      - MYSQL_DATABASE=example
      - MYSQL_ROOT_PASSWORD_FILE=/run/secrets/db-password
    expose:
      - 3306
  proxy:
    build: proxy
    ports:
      - 80:80
    depends_on: 
      - backend
volumes:
  db-data:
secrets:
  db-password:
    file: db/password.txt

这些是我的 dockerdocker-compose 版本:

Docker version 20.10.13, build a224086
docker-compose version 1.25.0, build unknown

知道我为什么会收到这些错误吗?当我尝试 运行 时,其他撰写文件会抛出类似的错误。我每天都在使用这个版本的 Docker 和 docker-compose 为其他项目成功。

根据 Zeitounator 的评论:

问题是我从 apt 安装了 docker-compose,而不是从官方存储库安装。该 apt 只有 1.25 版本,我相信它是最新的。事实上,目前最新的是 1.29。在官方文档中安装最新版本 as shown 后,它现在可以工作了。

pip install --upgrade docker-compose 没有为我更新。我不得不删除使用 apt 安装的版本,然后从官方 docker 仓库重新安装。

Compose file version 3 requires a version: label; see About versions and upgrading。您需要添加这一行,通常在文件的开头:

version: '3.8'

缺少此行,docker-compose 将文件解释为 version 1 Compose file。这省略了 top-level services: 键并将所有服务放在文件的顶层,但它也不支持 volumes:networks:,现代 Docker 网络不适用于 v1 Compose 文件。

可以说更新的Compose Specification makes version: optional。但是,删除它会使文件与现有工具不兼容,正如您在这个问题中看到的那样,我总是会包含它。