'Docker-Compose file has yaml.scanner.ScannerError

compose.yml file, which looks like this:

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

While I am executing it I get the following error:

yaml.scanner.ScannerError: mapping values are not allowed here
in "C:\...\docker-compose.yml", line 5, column 14

From what I see, nothing is wrong with the format, e.g. whitespaces missing. My overall goal is to specify a development mode docker-compose file, pointing it to the target directories from the different modules.
What am I doing wrong here?



Solution 1:[1]

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. :)

Solution 2:[2]

Ok, I wasted around 3 hours to debug a similar issue.

If you guys ever get the below error

ERROR: yaml.scanner.ScannerError: mapping values are not allowed here
in ".\docker-compose.yml", line 2, column 9

It's because a space is needed between

version:'3' <-- this is wrong

version: '3' <-- this is correct.

Also, if you are using eclipse, do yourself a favor and install the YEdit YAML editor plugin

Solution 3:[3]

And I forgot : after version

version '2'

Solution 4:[4]

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.

Solution 5:[5]

I met same issue and I think it's might be your yaml file format is invalid. So in order to solve it, first we need to make sure our yaml file format is valid

below is your original code snippet

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

If we use http://www.yamllint.com/ check the yaml file, we will get error as below: enter image description here

Then we change it as below:

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

after checking it, we will get result as below:

enter image description here

Solution 6:[6]

Bringing my answer because while I got the same error message, my solution was different.

Original docker-compose.yml

volumes: mongo:

After trying all the ideas in this thread I just went to the docker-compose docks and realized that volumes should be structured like:

volumes:
  mongo:

That's it for anyone else running into this.

Solution 7:[7]

Also make sure you have context and dockerfile at the same identation. I made a mistake and was stuck for hours.

My error was

ERROR: yaml.scanner.ScannerError: mapping values are not allowed here in "./docker-compose.yml", line 6, column 19

Wrong:

version : '3'
services:
  test:
    build:
      context: ./test
        dockerfile: Dockerfile.test
    image: kpod/test:2020

Right:

version : '3'
services:
  test:
    build:
      context: ./test
      dockerfile: Dockerfile.test
    image: kpod/test:2020

Solution 8:[8]

Hi further update to the given answer .... It's not specifically about the version: '2.0' line The statement

ERROR: yaml.scanner.ScannerError: mapping values are not allowed here

means there's a formatting error of some sort.

I got it and it was because I was missing a tab in my docker-compose file

version: '3.0'
services:
  mysql:
  image: ...

instead of

version: '3.0'
services:
  mysql:
    image: ...

Note the lack of an indenting tab on the image line

Solution 9:[9]

If you are using vs code do yourself a favor and install YAML extension by "RedHat".

Solution 10:[10]

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.

Solution 11:[11]

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

Solution 12:[12]

I found that missing one ":", was enough to produce the above error

Solution 13:[13]

I encountered a similar issue today, a syntax error in the docker-compose.yml file that caused the same error.

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

Removing this line // Add your services here fixed my issue

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

I hope this helps someone with a similar issue.

Solution 14:[14]

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

Solution 15:[15]

I wanted a volume mapped to a specific path on the external (host) server. I tried putting that under the top-level volumes entry in docker-compose.yml. After looking at the docker-compose file docs, I realized that that type of volume does not go there. Instead, it goes only under the volumes entry within the container definition. E.g.:

version: "3.7"
services:
  web:
    image: my_custom_web_image
    build: ./app
    volumes:
      - ./app/subdir:/usr/src/app/subdir

Solution 16:[16]

Check your whitespaces by validating your YAML input.

I spent 1hour to find out.

Solution 17:[17]

So, there is another reason! When you try to install r=Redash using the setup.sh from the Github, the script automatically gets the latest version of Redash and put it in docker-compose.yml. The base yml file doesn't have single-quotations (') around version name! As a result, you get an error that says:

ERROR: yaml.scanner.ScannerError: mapping values are not allowed here in "./docker-compose.yml", line 3, column 23 You just add single-quotation around the Redash version:

version: "2"
x-redash-service: &redash-service
  image: 'redash/redash:8.0.0.b32245'

Solution 18:[18]

check for the space between

ports:

- _space_ "8080:8080"

Solution 19:[19]

For me the fix was adding the space between version: '2'

Also useful, when stipulating what the context field is, using ./ or the actual absolute path where the Dockerfile resides resolved this error:

ERROR: build path /Users/name/somepath/someapp either does not exist, is not accessible, or is not a valid URL.