20

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
...
Basj
  • 41,386
  • 99
  • 383
  • 673

4 Answers4

21

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.

Xavier
  • 423
  • 4
  • 11
  • Thanks! I notice we can replace a multiline dash enumeration with `volumes: ["element1", "element2"]`. – Basj Oct 09 '20 at 07:20
  • 1
    Yes, they are the same, lists, they've been specified in the YAML syntax reference I linked in the answer above. – Xavier Oct 09 '20 at 07:23
  • Be careful that -SHOW="true" will consider the quotation marks as part of the string. – danieltc07 May 03 '22 at 13:11
2

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

DannyB
  • 12,810
  • 5
  • 55
  • 65
1

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>
mapto
  • 605
  • 9
  • 23
0

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.

Basj
  • 41,386
  • 99
  • 383
  • 673