我正在尝试使用 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

但我得到的是: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

不应该是 1.17 吗?我不明白。任何提示?

更新

我试图用键:值列表替换数组:
volumes:
  mydata:
    type: volume
    source: mydata
    target: /data
    volume:
      nocopy: true
  static:
    type: bind
    source: ./static
    target: /opt/app/static

但我得到的是以下内容:
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)

最佳答案

使用此语法,卷定义应该在使用它的服务内部,请参阅 official documentation

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:

否则,您可以使用特定驱动程序 like this 声明一个卷:
version: "3.2"
volumes:
  mydata:
    driver: local
    driver_opts:
      o: uid=500,gid=500

关于docker-compose 不接受我的卷声明,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45908162/

10-14 14:14
查看更多