skip to Main Content

In the docker-compose.yml files, why are certain enumerations done with a dash -, and others without?

services:
  web:           # the enumeration of [build, ports, volumes, environment] doesn't use a -
    build: .
    ports:             
      - "5000:5000"     # why the - here?  could we remove it and have ports: "5000:5000"?
    volumes:
      - .:/code         # why the - here?
    environment:
      FLASK_ENV: development        # why no - here?
  redis:
    image: "redis:alpine"

Another example:

version: '2'
services:
   db:
     image: mysql:5.7
     volumes:
       - ./mysql:/var/lib/mysql                # could we remove this - prefix?
     restart: always
     environment:
       MYSQL_ROOT_PASSWORD: somewordpress        # no - in this enumeration, why?
       MYSQL_DATABASE: wordpress
       MYSQL_USER: wordpress
       MYSQL_PASSWORD: wordpress
   wordpress:
     depends_on:
       - db                            # would it be ok without - ?
     image: wordpress:latest
     volumes:
       - ./wp:/var/www/html            # same
...

4

Answers


  1. Chosen as BEST ANSWER

    After further tests, I noticed that

    volumes: ./mysql:/var/lib/mysql 
    

    fails with:

    ERROR: The Compose file './docker-compose.yml' is invalid because:
    services.db.volumes contains an invalid type, it should be an array
    

    so it's either:

    volumes: 
        - ./mysql:/var/lib/mysql 
    

    or

    volumes: ['./mysql:/var/lib/mysql']
    

    according to YAML Multi-Line Arrays.


  2. According to: https://docs.ansible.com/ansible/latest/reference_appendices/YAMLSyntax.html

    Dashes represent Lists.

    All members of a list are lines beginning at the same indentation level starting with a -

    # A list of tasty fruits
    - Apple
    - Orange
    - Strawberry
    - Mango
    

    No Dashes Means that they are Key Value Pairs to a Dictionary.

    A dictionary is represented in a simple key: value form

    martin:
      name: Martin D'vloper
      job: Developer
      skill: Elite
    

    According to the Docker Compose Documentation at: https://docs.docker.com/compose/compose-file/compose-file-v2/

    environment:
      RACK_ENV: development
      SHOW: 'true'
      SESSION_SECRET:
    

    is the same as:

    environment:
      - RACK_ENV=development
      - SHOW=true
      - SESSION_SECRET
    

    Notice, that docker doesn’t care which one you use in the environment key , as long as they are consistent. Docker-compose syntax just happens to define it that way.

    Login or Signup to reply.
  3. This comes directly from YAML syntax specification.

    • Dashes/hyphens denote (unnamed) list elements, thus in the format -<value>
    • Entries without dashes are named child elements, format <name>: <value>
    Login or Signup to reply.
  4. The dash in YAML denotes an array of elements (values only).
    The other syntax in YAML, of key: value is a dictionary (key-value pairs).

    So, for example, ports is an array of values, with one value: "5000:5000". The : here has no significance from the YAML standpoint.

    As another example, and clarification about the environment directive in docker compose:

    The environment definition in fact supports two formats.

    When defined like this:

    environment:
      KEY: value
      KEY2: value2
    

    it is a YAML dictionary of key-value pairs.

    When defined like this:

    environment:
      - KEY=value
      - KEY2=value2
    

    it is an array of values, and once again, the = here has no significance from YAML standpoint, it is passed as is to the consumer that processes it (docker-compose binary in this case).

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search