Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

docker-compose.yml dash syntax in YAML

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
...
like image 403
Basj Avatar asked Oct 09 '20 07:10

Basj


2 Answers

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.

like image 189
Xavier Avatar answered Oct 11 '22 05:10

Xavier


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

like image 41
DannyB Avatar answered Oct 11 '22 05:10

DannyB