码头工人组成yaml.parser.ParserError

时间:2019-03-13 17:36:36

标签: docker docker-compose

使用以下docker-compose.yml,我总会收到无法解释的语法错误(我看不到两个docker-compose.ymls在第2行和第3行的区别)

---
version: '2'
  services:
    app-module:
      container_name: app-module:
      env_file: ./app-module:.env
      image: registry.x/app/app-module:latest
      network_mode: "bridge"
        ports:
        - "30303:30303"
      volumes:
      - type: volume
        source: node-volume
        target: /datadir
      - ./data:/data
      - ./log:/log

错误消息:

ERROR: yaml.parser.ParserError: while parsing a block mapping
  in "./docker-compose.yml", line 2, column 1
expected <block end>, but found '<block mapping start>'
  in "./docker-compose.yml", line 3, column 3

我看不到与其他工作文件的语法差异。

这就是我用来启发文件的docker-compose.yml:

---
version: '2'
services:
  app-node:
    container_name: app-node
    env_file: ./app-node.env
    image: registry.x/group/app-node:latest
    network_mode: "bridge"
    ports:
      - "7990:7990"
      - "7999:7999"
    volumes:
      - ./data:/data
      - ./log:/log

证明:

$ docker-compose config
services:
  app-node:
    container_name: app-node
    environment: {}
    image: registry.x/group/app-node:latest
    network_mode: bridge
    ports:
    - 7990:7990/tcp
    - 7999:7999/tcp
    volumes:
    - ...app-node/Test/data:/data:rw
    - ...app-node/Test/log:/log:rw
version: '2.0'

1 个答案:

答案 0 :(得分:3)

YAML中的空格很重要。 services:前面有两个不应存在的空格。您是在告诉YAML servicesversion中,但是version已经有一个值。

这是两者之间的区别:

foo: bar
  in_foo: bar

由于in_foo位于foo中而无法正常工作,并且:

foo: bar
not_in_foo: bar

之所以有效,是因为not_in_bar不在foo中。

或者,这将是有效的语法(但是docker-compose将会失败,因为它期望version中有一个字符串):

version:
  services:
    foo: bar