SkillSprout
Chapter 1 · Lesson 19
Beginner
1 min read

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

yaml
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

yaml
services:
  api:
    image: nginx:alpine
    ports:
      - "8080:80"
  • services is a mapping.
  • api is nested below services.
  • image and ports are nested below api.
  • ports contains a list with one item.

Indent each nested level consistently by two or four spaces. Do not use tabs.

Lists

yaml
environment:
  - APP_MODE=production
  - LOG_LEVEL=info

This list form has one string per item. Compose also accepts a mapping form:

yaml
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:

yaml
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

bash
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