YAML for Docker Compose
Overview
Compose files use YAML, a structured text format. YAML stores mappings as key: value, lists as - item, and nesting through indentation. It is visually simple but whitespace-sensitive.
Detailed Notes
Mapping
name: orders-api
image: nginx:alpine
enabled: true
Each line is a mapping. name contains orders-api, image contains nginx:alpine, and enabled contains a boolean.
Nested mappings
services:
api:
image: nginx:alpine
ports:
- "8080:80"
servicesis a mapping.apiis nested belowservices.imageandportsare nested belowapi.portscontains a list with one item.
Indent each nested level consistently by two or four spaces. Do not use tabs.
Lists
environment:
- APP_MODE=production
- LOG_LEVEL=info
This list form has one string per item. Compose also accepts a mapping form:
environment:
APP_MODE: production
LOG_LEVEL: info
Both forms describe environment variables. Mapping form is easier to read for a beginner.
Quotes
Port values containing a colon should usually be quoted:
ports:
- "8080:80"
Values that look like numbers or booleans can sometimes change type if unquoted. Quoting a port string prevents YAML from interpreting it unexpectedly.
Validate before starting
docker compose config
docker compose up
docker compose config renders and validates the file, including variable expansion. Fix YAML or key errors before starting containers.
Common Interview Questions
Finished this lesson?
0 of 27 lessons done · 0% of the course

