Yaml: line 3: mapping values are not allowed in this context

Hello! I’m a beginner and I’m trying to use docker compose to build 2 websites for me. When trying to build the docker-compose.yaml file, I keep getting this error:

yaml: line 3: mapping values are not allowed in this context

I’ve validated the file using YAMLint and it says it’s valid but I still get the error.

Here’s my code:

version: '3.8'
services:
  web-app1:
    build: ./website1
    ports:
    - 9000:80
  web-app2:
    build: ./website2
    ports:
    - 5000:50

Any help would be appreciated. Thank you.

A. The version top-level property is deprecated, it is only used for the old docker-compose tool, before it was integrated as docker compose

B. Aside from that, the YML looks fine, are you sure that is all there is?

I see no problem with line 3

The error message is not about yaml syntax. Then you would get parse error (or maybe in some cases exactly what you got) . A yaml could be valid and still incorrect compose file. But @deanayalon is right this file is correct. Try this file in an empty folder. Maybe you have an override file or you forgot to set the compose file path in the command line and a default compose file was loaded which is not what you shared. It can even happen if you for example have this file structure:

.
├── compose.yml
└── test
    └── compsoe.yml

And you have the correct value in test/compsoe.yml, but it is incorrectly named and you run the compose command in the test folder assuming that will be used, but compose will search for the compose file in the parent folders as well. If then you have this in the correctly named compose file next to the test folder:

# first line
 :'3.8'
services:
  web-app1:
    build: ./website1
    ports:
    - 9000:80
  web-app2:
    build: ./website2
    ports:
    - 5000:50

You will get

 yaml: line 3: mapping values are not allowed in this context

Thanks for the help! I solved the issue although I’m still not quite sure what it was. I removed the version line and I still had the error come up. In the end, I just deleted the yaml file and rewrote it again, exactly the same as before, didn’t move anything around, and suddenly it worked. Perhaps it was a space or something? Either way, got it working. Thanks again.

At first I guessed it may be a space, but having copy-pasted your YAML, there was no issue on my end
Whatever, so long as it ended up working :slight_smile: