'Docker stack deploy error about top-level object mappings

Following along with the Docker getting started guide, https://docs.docker.com/get-started/part3/#your-first-docker-composeyml-file, 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.



Solution 1:[1]

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.

Solution 2:[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:

Solution 3:[3]

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

Solution 4:[4]

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.

Solution 5:[5]

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:

Solution 6:[6]

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

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

Solution 7:[7]

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.

Solution 8:[8]

Probably you forgot to save the docker compose file.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 bytec0de
Solution 2 Kamal
Solution 3 Carra
Solution 4 Yiannis Giannakopoulos
Solution 5 rstreet
Solution 6 Serzhan Akhmetov
Solution 7 Janeks Malinovskis
Solution 8 Alexandre