Service container_name must be a mapping not a string

Post Views : 2

Question

This Content is from Stack Overflow. Question asked by natukita

When I ran: docker-compose up -d, I got error saying

“ERROR: In file ‘./docker-compose.yml’, service ‘container_name’ must be a mapping not a string.”

This is my yml file:

version'3' services:
    web:
    container_name: nginx1
    ports:
             - "8080:80"
    image: nginx

~

Solution

For more tutorials visit Jtuto.com


This Question and Answer are collected from stackoverflow and tested by JTuto community, is licensed under the terms of CC BY-SA 4.0.

people found this article helpful. What about you?


Question

Hi, I am trying to use the example provided in the site https://docs.docker.com/compose/gettingstarted/

Step 3: Define services Define a set of services using docker-compose.yml:

Create a file called docker-compose.yml in your project directory and add the following:

version: '2'
services:
  web:
    build: .
    ports:
     - "5000:5000"
    volumes:
     - .:/code
    depends_on:
     - redis
  redis:
    image: redis

and when I run the command docker-compose up i got the below error

ERROR: yaml.scanner.ScannerError: while scanning for the next token found character ‘\t’ that cannot start any token in “./docker-compose.yml”, line 18, column 1 so i removed all spaces and modified it as below

Defines two web services, web and redis

version: ‘2’ services: web: build: . #Build from the dockerfile from current dir ports:

  • “5000:5000” #Forward the exposed port 5000 on the container to port 5000 on the host machine volumes:
  • .:/code #Mounts the project directory on the host to /code inside the container directory allowing you to modify the code without having to rebuild the image depends_on:
  • redis redis: image: redis #Links the webservice to the redis service

on trying it again i am getting an error now as

docker-compose up ERROR: In file ‘./docker-compose.yml’, service must be a mapping, not a NoneType.

Can I get some help what is wrong here… I am using MAC.


Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer


These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

I'm getting issue when running docker-compose.yml file and below is the script written in the file 

version: '3.3'
services:
 db:
 image: mysql:5.7
 volumes:
 - db_data:/var/lib/mysql
 restart: always
 environment:
 MYSQL_ROOT_PASSWORD: somewordpress
 MYSQL_DATABASE: wordpress
 MYSQL_USER: wordpress
 MYSQL_PASSWORD: wordpress
 wordpress:
 depends_on:
 - db
 image: wordpress:latest
 ports:
 - "8000:80"
 restart: always
 environment:
 WORDPRESS_DB_HOST: db:3306
 WORDPRESS_DB_USER: wordpress
 WORDPRESS_DB_PASSWORD: wordpress
volumes:
 db_data: 

Below is my error:

ERROR: In file './docker-compose.yml', service 'image' must be a mapping not a string.

Please help.

I want to use the existing network "nextcloudpi":

$ sudo docker network ls
NETWORK ID     NAME                               DRIVER    SCOPE
bc60ac79af99   bridge                             bridge    local
e374ccc22ff4   host                               host      local
6822ac7b54c6   ncp_reverse_proxy_bc_nextcloudpi   bridge    local
dc749eca4598   ncp_reverse_proxy_nextcloudpi      bridge    local
6a83b3f6e623   nextcloudpi                        bridge    local
eeb4fc6c4f16   none                               null      local
79949d81c193   overleaf_default                   bridge    local

I get the error:

In file './docker-compose.yml', service 'networks' must be a mapping not an array

version: '2.2'


networks:
  nextcloudpi:
    external: true


services:
    sharelatex:
        restart: always
        # Server Pro users:
        # image: quay.io/sharelatex/sharelatex-pro
        image: sharelatex/sharelatex
        container_name: sharelatex
        depends_on:
            mongo:
                condition: service_healthy
            redis:
                condition: service_started
        ports:
            - 83:80
        links:
            - mongo
            - redis
        networks:
           - nextcloudpi

asked Jul 4 at 15:36

2

This file is working as expected:

version: '2.2'
  

services:
    sharelatex:
        restart: always
        # Server Pro users:
        # image: quay.io/sharelatex/sharelatex-pro
        image: sharelatex/sharelatex
        container_name: sharelatex
        depends_on:
            mongo:
                condition: service_healthy
            redis:
                condition: service_started
        ports:
            - 83:80
        links:
            - mongo
            - redis
        

networks:
  default:
    name: nextcloudpi
    external: true

answered Jul 5 at 9:36