Docker-Compose file has yaml.scanner.ScannerError

后端 未结 17 2097
情话喂你
情话喂你 2021-02-01 12:04

compose.yml file, which looks like this:

version: \'2\'
services:
  discovery-microservice:
    build: discovery-microservice
      context: /discov         


        
相关标签:
17条回答
  • 2021-02-01 12:39

    Literally found the solution seconds later. You have to remove the "discovery-microservice" after "build":

    version: '2'
    services:
      discovery-microservice:
        build:
          context: ./discovery-microservice/target/docker
          dockerfile: Dockerfile
        ports:
         - "8761:8761"
    

    Also you can use "./" in context for relative paths. :)

    0 讨论(0)
  • 2021-02-01 12:39

    What is wrong is that here:

        build: discovery-microservice
    

    you start a mapping which has a key build indented by four spaces. The value for that key is a scalar that starts with discovery-microservice and possible continues to the next line, depending on whether that has a key: value pair at the same indentation level or not

    What is not allowed inside such a multiline scalar is that you have an unquoted : and have that at a different indentation level. Which is exactly what you do have.

    The parser seeing context indented at a different level than build assumes you are writing a scalar string discovery-microservice context which cannot be followed on the same line (as context) by a colon.

    Apart from removing discovery-microservice after build as you did in your answer, what would also make this valid YAML (but with a different meaning, probably non-sense for docker compose) are:

    services:
      discovery-microservice:
        build: "discovery-microservice
          context: /discovery-microservice/target/docker"
    

    and

    services:
      discovery-microservice:
        build: discovery-microservice
        context: /discovery-microservice/target/docker"
    

    For docker-compose version 2 files, the build key expects a mapping (and not a scalar as in the "solutions" above), making your answer the correct way to solve this.

    0 讨论(0)
  • 2021-02-01 12:43

    1) Give a space after every colon whenever you do mapping after defining key.

    2) A YAML file uses 2(two spaces or tabs) indentation. -->It means that after every line you need to use two tabs,when you write a sentence in following line. I hope this will make easy to write any YAML file now.

    0 讨论(0)
  • 2021-02-01 12:43

    check for the space between

    ports:

    - _space_ "8080:8080"

    0 讨论(0)
  • 2021-02-01 12:45

    Using the vs code yaml RedHat extension, I saw I was off by one indent:

    Wrong:

    version: '3'
    
    services:
    
      web:
        image: nginx
         ports:
        - 9090:80
    
      database:
        image: redis
    

    Right:

    version: '3'
    
    services:
    
      web:
        image: nginx
        ports:
        - 9090:80
    
      database:
        image: redis
    
    0 讨论(0)
  • 2021-02-01 12:45

    Another possible culprit can be stray tabs at the end of the file, which I learned today.

    0 讨论(0)
提交回复
热议问题