Following along with the Docker getting started guide, , I'm running into an issue. I've created the docker-compose.yml file and verified that the contents are correct:

version: "3" services: web: image: joshuabelden/get-started:part2 deploy: replicas: 5 resources: limits: cpus: "0.1" memory: 50M restart_policy: condition: on-failure ports: - "80:80" networks: - webnet networks: webnet: 

I also verified that I can run my image outside of a swarm. After running the command:

 docker stack deploy -c docker-compose.yml getstartedlab 

I'm getting the following error:

Top-level object must be a mapping 

I can't seem to find any information on the error message.

3

16 Answers

What I did to solve this is I removed the double quotes and made them single quotes to change version: "3" -> version: '3' This removed the error for me, also do this for all double quotes.

4

You probably didn't save after modifying the docker-compose.yml file. So if you run 'docker compose up' without having saved, you get the error about top-level object mappings.

2

You have to add the "volumes" where your code should be copied:

version: "3" services: web: image: iconkam/get-started:part2 deploy: replicas: 5 resources: limits: cpus: "0.1" memory: 50M restart_policy: condition: on-failure volumes: - .:/app ports: - "80:80" networks: - webnet networks: webnet: 
1

I am using Symfony 6 on Linux Ubuntu and I had the same type of problem but not with "docker stack". I had this problem simply when running: docker-compose up

$ docker-compose up Top-level object must be a mapping 

I searched for a long time to find a solution. No solution described here worked.

In fact the problem was caused by the presence of a default file in Symfony 6:

docker-compose.override.yml 

I commented all the content inside but it was not enough.

Renaming the file made the command "docker-compose up" worked. Deleting the file is also a solution ;-)

This error arises from the formatting of the file. Please try to convert the file encoding to UTF-8 and you will be able to run docker stack deploy command. Double quotes is not an issue here.

This happens when Docker is running in Kubernetes mode and not swarm.

I fixed it by changing it to Swarm through settings > Kubernetesenter image description here

In my case, I wrapped all the values in double quotes expect the replica, and it got fixed. Like so:

version: "3" services: web: # replace username/repo:tag with your name and image details image: "image details" deploy: replicas: 5 resources: limits: cpus: "0.1" memory: "50M" restart_policy: condition: "on-failure" ports: - "4000:80" networks: - "webnet" networks: webnet: 

Probably you forgot to save the docker compose file.

I just had this problem and the reason was that I had an empty docker-compose.override.yml

1

Sometimes it's just the formatting in the file. I recommend selecting your text in the compose file and seeing if you have a trailing blank space somewhere.

In my case, I had a space right after the image tag.

Just close and reopen Terminal again - and run command again.

p.s. I'm using Windows + WSL Terminal. Time to time randomly see same error.

2

Another reason this may be happening while you're pulling your hair out:

You have the environment variable COMPOSE_FILE set, which refers to a file which is empty or an invalid yml file.

In my case it was set to docker-compose.combined.yml while running docker compose config > docker-compose.combined.yml, which first created an empty file to write to, and then stated the error Top-level object must be a mapping while trying to read that empty file.

I got the same error in vs code when the autosaving crashed for some reason when trying to save the docker-compose.yml file In order to resolve it you need to delete the file and recreated it again (copy the contains before deletion xd)

I hope this helps

1

Seems this is because of yml file having formatting issues. I just copied everything from this file and pasted into a newly created file using notepad++/notepad and saved. Later, I used that file to run docker-compose command. It worked smoothly.

2

I have used PyCharm and it generates a command to run: docker-compose -f Dockerfile up.
The problem is that for some reason this error occurs with the -f flag, but without it everything is fine.

Another thing is that Docker does not recognize the file because it is not saved. Try saving the file and get rid of the quotations and putting them as single apostrophes and that should do the trick.

Saving the file is what solved the error for me.

1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.