TExcelTAD
Home › Docker Compose › The Compose File

The Compose File

A Compose file is a YAML document that follows the Compose Specification, an open format that Docker Compose, Docker Swarm (for stacks) and other tools understand. In this lesson you will learn its overall shape: where Compose looks for it, which top-level sections exist, how YAML types matter, and how to read the canonical form that docker compose config prints.

Where Compose looks

Run in a folder, Compose searches for these names in order and uses the first one it finds:

Priority File name Note
1 compose.yaml preferred
2 compose.yml
3 docker-compose.yaml older name, still supported
4 docker-compose.yml older name, still supported

If the folder has no file, Compose walks up through parent folders until it finds one, so docker compose ps works from a subfolder of the project too. Next to the main file, an optional compose.override.yaml is merged automatically (see Override Files & Merging). With -f you choose files explicitly, and -f - reads the file from standard input.

The top-level sections

text
compose.yaml
├── name:       project name (optional; default = folder name)
├── include:    other Compose files to pull in          → include lesson
├── services:   the containers to run                   → most of this course
├── networks:   networks the services attach to         → Networks lesson
├── volumes:    named volumes                           → Named Volumes lesson
├── configs:    non-secret files mounted into services  → Configs & Secrets lesson
├── secrets:    sensitive files mounted into services   → Configs & Secrets lesson
└── x-…:        your own extension fields (ignored by Compose)

Only services is really needed, and even an empty services: {} is a valid project. There is no version key any more: if you leave an old version: "3.8" at the top, Compose ignores it and prints a warning (the examples' project folder is /home/ada/demo):

compose · try it yourself
version: "3.8"
services:
  web:
    image: nginx:1.29
WARN[0000] /home/ada/demo/compose.yaml: the attribute `version` is obsolete, it will be ignored, please remove it to avoid potential confusion
name: demo
services:
  web:
    image: nginx:1.29
    networks:
      default: null
networks:
  default:
    name: demo_default

Here is a file that uses most top-level sections. Read the output section by section:

compose · try it yourself
# $ docker compose config --no-path-resolution
name: shop
x-owner: platform-team

services:
  api:
    image: shop/api:1.4
    networks: [backend]
    volumes:
      - uploads:/srv/uploads
    configs:
      - app_config
    secrets:
      - db_password
  db:
    image: postgres:18
    networks: [backend]
    volumes:
      - pgdata:/var/lib/postgresql

networks:
  backend: {}

volumes:
  pgdata:
  uploads:

configs:
  app_config:
    file: ./config/app.toml

secrets:
  db_password:
    file: ./secrets/db_password.txt
name: shop
services:
  api:
    configs:
      - source: app_config
    image: shop/api:1.4
    networks:
      backend: null
    secrets:
      - source: db_password
        target: /run/secrets/db_password
    volumes:
      - type: volume
        source: uploads
        target: /srv/uploads
        volume: {}
  db:
    image: postgres:18
    networks:
      backend: null
    volumes:
      - type: volume
        source: pgdata
        target: /var/lib/postgresql
        volume: {}
networks:
  backend:
    name: shop_backend
volumes:
  pgdata:
    name: shop_pgdata
  uploads:
    name: shop_uploads
secrets:
  db_password:
    name: shop_db_password
    file: ./secrets/db_password.txt
configs:
  app_config:
    name: shop_app_config
    file: ./config/app.toml
x-owner: platform-team

Things to notice:

  • Resource names get the project prefix: the backend network is really shop_backend, the volume pgdata is shop_pgdata. Inside the file you always use the short key.
  • No default network this time. Both services joined backend explicitly, so nothing needs shop_default and Compose doesn't create it.
  • Short forms are expanded: uploads:/srv/uploads became a type: volume mount, and the secret got its default target /run/secrets/db_password.
  • The x-owner extension field survives untouched at the end. Compose ignores any key starting with x-, which makes such keys perfect for notes and for reusable YAML fragments.
  • --no-path-resolution keeps relative paths such as ./config/app.toml as written. Without it Compose turns them into absolute paths based on the project folder.

Listing parts of a project

config has flags that print just one kind of thing. They are handy in scripts (for example, to build or scan every image in a project):

compose · try it yourself
# $ docker compose config --services
# $ docker compose config --volumes
# $ docker compose config --networks
services:
  web:
    image: nginx:1.29
    networks: [front]
  api:
    image: shop/api:1.4
    networks: [front, back]
    volumes: [cache:/var/cache/api]
  db:
    image: postgres:18
    networks: [back]
    volumes: [pgdata:/var/lib/postgresql]
networks:
  front:
  back:
volumes:
  cache:
  pgdata:
$ docker compose config --services
api
db
web
$ docker compose config --volumes
cache
pgdata
$ docker compose config --networks
back
front

YAML types matter

Compose files are YAML, so indentation defines structure and every value has a type. Compose's YAML parser follows YAML 1.2, but most fields in the specification are strings, and Compose converts what you write into the type the field needs. The conversion is usually what you want, with one notable trap:

compose · try it yourself
services:
  app:
    image: alpine:3.22
    environment:
      DEBUG: true
      RETRIES: 3
      RATIO: 0.50
      VERSION: 1.10
      ZIP: "01234"
name: demo
services:
  app:
    environment:
      DEBUG: "true"
      RATIO: "0.5"
      RETRIES: "3"
      VERSION: "1.1"
      ZIP: "01234"
    image: alpine:3.22
    networks:
      default: null
networks:
  default:
    name: demo_default

Every environment value became a string, because container variables are always strings. But YAML first read 0.50 and 1.10 as numbers, so they arrived as "0.5" and "1.1"; the trailing zero is gone before Compose ever sees it. A version string 1.10 silently turning into 1.1 is a real bug. Quote anything that must be kept exactly, like ZIP: "01234".

Comments, multi-line strings and anchors

A few YAML features you will see in real Compose files:

yaml
services:
  worker:
    image: shop/worker:1.4
    # a comment: everything after # to the end of the line
    command: >
      python -m worker
      --queue emails
      --concurrency 4
    labels:
      description: |
        Sends transactional email.
        Owned by the platform team.
  • > (folded) joins the lines with spaces: the command is one line.
  • | (literal) keeps the newlines: handy for inline Dockerfiles and config snippets.
  • Anchors (&name) and aliases (*name) let you reuse blocks; they get their own lesson, YAML Anchors & Extensions.

Validation

Compose checks the file against the specification's JSON schema. A misspelled key fails loudly rather than being ignored:

compose · try it yourself
services:
  web:
    imagee: nginx:1.29
validating /home/ada/demo/compose.yaml: services.web.imagee false schema

The message names the path of the offending key; false schema means the schema allows nothing under that name. (Compose releases before v5.6 worded it services.web additional properties 'imagee' not allowed.)

A YAML syntax error, such as a missing closing quote, is caught even earlier, by the parser:

compose · try it yourself
services:
  web:
    image: nginx:1.29
    ports:
      - "8080:80
go-yaml load error in scanner (while scanning a quoted scalar) at L5.C9-L6.C1: found unexpected end of stream

Make docker compose config -q (quiet: no output on success, only errors) the first step of every CI pipeline that touches a Compose file. It costs a fraction of a second.

A model of the file lookup

The model replays Compose's search for a project file: check the four names in the current folder, then move up one folder and try again.

python · try it yourself
NAMES = ["compose.yaml", "compose.yml", "docker-compose.yaml", "docker-compose.yml"]

def find_project_file(cwd, existing):
    folder = cwd
    while True:
        for name in NAMES:
            path = f"{folder.rstrip('/')}/{name}"
            if path in existing:
                return path
        if folder == "/":
            return "no configuration file provided: not found"
        folder = folder.rsplit("/", 1)[0] or "/"

files = {"/home/ana/shop/docker-compose.yml", "/home/ana/shop/compose.yaml", "/home/ana/blog/docker-compose.yml"}
print(find_project_file("/home/ana/shop", files))
print(find_project_file("/home/ana/shop/api/src", files))
print(find_project_file("/home/ana/blog", files))
print(find_project_file("/tmp", files))
/home/ana/shop/compose.yaml
/home/ana/shop/compose.yaml
/home/ana/blog/docker-compose.yml
no configuration file provided: not found

When both compose.yaml and docker-compose.yml exist, Compose uses the first and warns that it found several candidates. Keep exactly one.

Learn more: The Compose CLI · Services: image & build · Stack files in Swarm

Test yourself

Which file name does Compose prefer if several exist in the folder?

  1. docker-compose.yml
  2. compose.yaml
  3. compose.override.yaml
  4. docker-compose.yaml

What does VERSION: 1.10 under environment become in the container?

  1. "1.10"
  2. "1.1"
  3. An error
  4. 1.10 as a number

What does Compose do with a top-level key named x-defaults?

  1. Rejects it
  2. Treats it as a service
  3. Ignores it (an extension field), so you can use it for notes and anchors
  4. Merges it into every service