docker-compose 不接受我的卷声明

Ber*_*tuz 7 docker docker-compose

我正在尝试使用 docker-compose。我首先从 docker 文档中复制了一个示例:

version: '3'
services:
  web:
    image: nginx:alpine
    ports:
      - "80:80"

networks:
  webnet:

volumes:
  - type: volume
    source: mydata
    target: /data
    volume:
      nocopy: true
  - type: bind
    source: ./static
    target: /opt/app/static
Run Code Online (Sandbox Code Playgroud)

但我得到的是:ERROR: In file './docker-compose.yml', volume must be a mapping, not an array.

在我看来,这与旧版本的 docker-compose 有关。所以我尝试更新在 MacO 上运行的 docker,但它是最新的。通过检查版本,这是我得到的:

Matteos-MacBook-Pro-2:chateo matteo$ docker-compose -v   
docker-compose version 1.14.0, build c7bdf9e
Run Code Online (Sandbox Code Playgroud)

不应该是1.17吗?我不明白。有什么提示吗?

更新

我尝试用键:值列表替换数组:

volumes:
  mydata:
    type: volume
    source: mydata
    target: /data
    volume:
      nocopy: true
  static:
    type: bind
    source: ./static
    target: /opt/app/static
Run Code Online (Sandbox Code Playgroud)

但我得到的是以下内容:

Matteos-MacBook-Pro-2:chateo matteo$ docker-compose build
ERROR: The Compose file './docker-compose.yml' is invalid because:
volumes.static value Additional properties are not allowed ('source', 'type', 'target' were unexpected)
volumes.mydata value Additional properties are not allowed ('volume', 'source', 'type', 'target' were unexpected)
Run Code Online (Sandbox Code Playgroud)

Mat*_*ieu 5

使用此语法,卷定义应该位于使用它的服务内部,请参阅官方文档

version: "3.2"
services:
  web:
    image: nginx:alpine
    volumes:
      - type: volume
        source: mydata
        target: /data
        volume:
          nocopy: true
      - type: bind
        source: ./static
        target: /opt/app/static

  db:
    image: postgres:latest
    volumes:
      - "/var/run/postgres/postgres.sock:/var/run/postgres/postgres.sock"
      - "dbdata:/var/lib/postgresql/data"

volumes:
  mydata:
  dbdata:
Run Code Online (Sandbox Code Playgroud)

否则,您可以声明具有特定驱动程序的卷,如下所示

version: "3.2"
volumes:
  mydata:
    driver: local
    driver_opts:
      o: uid=500,gid=500
Run Code Online (Sandbox Code Playgroud)


Buk*_*gey 0

尝试将定义更改volumes为如下所示:

volumes:
  mydata:
    type: volume
    source: mydata
    target: /data
    volume:
      nocopy: true
  static:
    type: bind
    source: ./static
    target: /opt/app/static
Run Code Online (Sandbox Code Playgroud)

我可能是错的,但卷不能是数组。