Docker stack deploy error about top-level object mappings

Viewed 20611

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.

9 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.

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:

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

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.

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:

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.

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.

Probably you forgot to save the docker compose file.

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.

Related