From point A to point B
Point A: you have already written compose.yaml files by copying examples, but when something fails you cannot tell whether the problem is a space, a dash or a pair of quotes. Point B: by the end of this extra chapter you will read and write YAML with ease, tell dictionaries, lists and lists of dictionaries apart, avoid their most common traps and validate a file before you use it.
Every example sits in a code block with a Copy button: paste it into a file and experiment.
What YAML is: XML, JSON and YAML with the same data
YAML (YAML Ain't Markup Language) is a data serialization format: a way of writing structured data as text so that both people and programs can read it. It is used above all for configuration files: Docker Compose, Kubernetes, Ansible, GitHub Actions or the Helm values.yaml files you saw with Cilium.
The best way to understand it is to compare. These three blocks describe exactly the same data: a list of servers, each with its properties.
<servers>
<server>
<name>router-01</name>
<owner>ana</owner>
<created>2026-01-15</created>
<status>active</status>
</server>
</servers>
{
"servers": [
{
"name": "router-01",
"owner": "ana",
"created": "2026-01-15",
"status": "active"
}
]
}
servers:
- name: router-01
owner: ana
created: 2026-01-15
status: active
XML needs opening and closing tags; JSON needs braces, brackets, quotes and commas. YAML expresses the same structure with nothing but indentation, colons and dashes. That is why it is the favorite for files that people edit.
Key-value pairs
The basic building block is the key: value pair. After the colon there is always one space.
hostname: router-01
vendor: Cisco
ports: 48
enabled: true
mgmt_ip: 10.0.0.1
YAML works out the type of each value: 48 is a number, true a boolean and the rest is text. Without the space after the colon (hostname:router-01), YAML does not see a key-value pair, just a loose piece of text.
Lists
A list (also called an array or sequence) is a collection of items. Each item starts with a dash and a space, at the same indentation:
protocols:
- OSPF
- BGP
- IS-IS
vlans:
- 10
- 20
- 30
For short lists there is also the inline form, identical in meaning. You saw it in chapter 5 with the Compose networks:
protocols: [OSPF, BGP, IS-IS]
networks: [front-end, back-end]
Dictionaries and indentation
A dictionary (or map) groups several key-value pairs under one key. What decides which properties belong to what is indentation: everything further to the right than router-01 belongs to it.
router-01:
vendor: Cisco
model: ISR 4331
ports: 3
switch-01:
vendor: Arista
model: 7050X
ports: 48
Indentation sets the hierarchy: two spaces further right means "property of". Diagram: The House of Routing.
Properties at the same level must have exactly the same indentation. See what happens when one is out of line:
# Correct: vendor, model and ports are siblings
router-01:
vendor: Cisco
model: ISR 4331
ports: 3
# Wrong: "model" has more spaces and YAML cannot read it
router-01:
vendor: Cisco
model: ISR 4331
ports: 3
The second block gives a syntax error (mapping values are not allowed here). And if a key ends up with less indentation, it stops belonging to router-01 without any warning: the hardest error to find.
.yaml files.Dictionary, list and list of dictionaries
Real files combine all three structures. Choosing the right one depends on what you are describing.
One object with properties: dictionary
A single router, with simple properties and one property that is itself a dictionary (model):
hostname: core-01
model:
manufacturer: Cisco
series: ASR 1001-X
role: core
price: 18000
Several values of the same kind: list
hostnames:
- core-01
- core-02
- edge-01
Several objects with properties: list of dictionaries
When you have several routers and each one has its own properties, you use a list whose items are dictionaries. The dash marks where each router starts:
routers:
- hostname: core-01
model:
manufacturer: Cisco
series: ASR 1001-X
role: core
price: 18000
- hostname: core-02
model:
manufacturer: Cisco
series: ASR 1001-X
role: core
price: 18000
- hostname: edge-01
model:
manufacturer: Juniper
series: MX204
role: edge
price: 22000
Look at the indentation: model, role and price line up with hostname, not with the dash. They are all properties of the same item.
Dictionary: one object. List: several values. List of dictionaries: several objects. Diagram: The House of Routing.
compose.yaml, services is a dictionary of services, ports is a list, and the containers of a Kubernetes Pod are a list of dictionaries: each container with its name, image and ports.Order and comments
In a dictionary, order does not matter: these two blocks mean the same thing, because each value is looked up by its key.
# Version A
router-01:
vendor: Cisco
ports: 3
model: ISR 4331
# Version B (another file): the same meaning
router-01:
model: ISR 4331
vendor: Cisco
ports: 3
In a list, order does matter: these two blocks are different. In a list of DNS servers or firewall rules, the order decides which one is tried first.
# Version A
dns:
- 10.0.0.53
- 1.1.1.1
# Version B (another file): a different list
dns:
- 1.1.1.1
- 10.0.0.53
Everything after a # is a comment and is ignored. Use comments to explain the why behind a configuration:
# DNS servers, in order of preference
dns:
- 10.0.0.53 # internal resolver
- 1.1.1.1 # external fallback
Common traps
YAML tries to guess the type of each value, and sometimes it guesses wrong. These are the most frequent surprises:
| You write | YAML understands | Fix |
|---|---|---|
version: 1.20 | The number 1.2 | version: "1.20" |
mode: 0755 | The octal number 493 in some parsers (YAML 1.1) | mode: "0755" |
country: NO | The boolean false in some parsers (YAML 1.1) | country: "NO" |
enabled: on | The boolean true in some parsers | Use true or false |
message: hello: world | Error: a colon inside the value | message: "hello: world" |
ports: - "80:80" on the same line | Syntax error | The dash goes on the next line, indented |
When in doubt, put the value in quotes: quoted text is always text.
Multi-line text
# | keeps the line breaks (ideal for scripts or certificates)
script: |
apt-get update
apt-get install -y curl
# > joins the lines into one (ideal for long descriptions)
description: >
Edge router for the main data center,
connected to two transit providers.
Several documents in one file
Three dashes separate independent documents. Kubernetes uses this a lot to put several objects in a single file:
apiVersion: v1
kind: Namespace
metadata:
name: lab
---
apiVersion: v1
kind: ConfigMap
metadata:
name: app-config
namespace: lab
data:
ENVIRONMENT: testing
Lab: validate your YAML
Before you use a file, check it. Create network.yaml with this content:
# network.yaml
site: head-office
routers:
- hostname: core-01
loopback: 10.255.0.1
vlans: [10, 20, 30]
- hostname: edge-01
loopback: 10.255.0.2
vlans: [99]
dns:
- 10.0.0.53
- 1.1.1.1
Validate it and convert it to JSON with yq, without installing anything but Docker:
docker run --rm -v "$PWD":/workdir mikefarah/yq -o=json '.' network.yaml
# Queries: the hostname of the second router and the VLANs of the first
docker run --rm -v "$PWD":/workdir mikefarah/yq '.routers[1].hostname' network.yaml
docker run --rm -v "$PWD":/workdir mikefarah/yq '.routers[0].vlans' network.yaml
Or with Python, if you have the PyYAML library installed:
python3 -c 'import sys, yaml, json; print(json.dumps(yaml.safe_load(open(sys.argv[1])), indent=2))' network.yaml
Now break it on purpose: change the indentation of loopback in the first router, or replace two spaces with a tab, and validate again. Read the error message: it gives the line and column of the problem.
docker compose config validates your compose.yaml and shows the final result, with the variables already substituted. Use it before every up whenever you change the file.YAML cheat sheet
| You need | Syntax |
|---|---|
| A value | key: value (space after the colon) |
| A list | - item on each line, or [a, b, c] |
| A dictionary | Properties indented two more spaces |
| A list of dictionaries | - key: value and the rest aligned with the first key |
| A comment | # text |
| Force text | "1.20", "NO", "hello: world" |
| Multiple lines | | keeps line breaks; > joins them |
| Multiple documents | --- between them |
Challenge
Answer before you open the solution. Getting it wrong here is the cheapest way to learn.
1. What is wrong with hostname:router-01?
The space after the colon is missing. Without it, YAML does not read it as a key and a value.
2. You change the order of a dictionary's properties. Does the meaning change?
No. In a dictionary, order does not matter. In a list, it does.
3. You need to describe five switches, each with a hostname, model and IP. Which structure do you use?
A list of dictionaries: one item with a dash per switch, and its three properties aligned underneath.
4. You write version: 1.10 and the program reads 1.1. Why, and how do you avoid it?
YAML reads it as a number and drops the trailing zero. Put it in quotes: version: "1.10".
5. Your file throws an error and the indentation looks right at a glance. What is the first thing you check?
That there are no tabs. YAML does not allow them for indentation, and in the editor they look just like spaces.
What you take away
- YAML is a human-readable data serialization format; it is the foundation of Compose, Kubernetes and Ansible.
- Three pieces:
key: value, lists with-and dictionaries with indentation. - Indentation defines the hierarchy: spaces only, always the same number.
- A dictionary for one object, a list for several values, a list of dictionaries for several objects.
- Dictionaries have no order; lists do.
#starts a comment. - When in doubt, use quotes. And always validate:
yq, Python ordocker compose config.
With this you complete Docker from Scratch. The next step on the path is Kubernetes, where you will write YAML every day.
Official resources
- YAML 1.2.2 specificationyaml.org
- The Official YAML Web Siteyaml.org
- yqgithub.com
- docker compose configdocs.docker.com
- Kubernetes objects in YAMLkubernetes.io
Source and credits. Chapter written from Jesús Espinoza's study notes on KodeKloud's Docker for Absolute Beginners course (December 2024). Text drafted with AI assistance; original diagrams by The House of Routing. Technical details were checked against the official Docker documentation.