Skip to content
Novus Examples
yaml1.1 KB

Docker Compose Anchors and x- Extension Fields

Compose with three x- extension fields carrying YAML anchors that are merged into services. The trap is ordering: a tool that strips x- keys before parsing the YAML destroys the anchors and the file stops resolving.

Preview — first 45 linesyaml
# Compose with x- extension fields and YAML anchors. Both are load-bearing here: `x-` keys are
# ignored by Compose but are ordinary YAML, and the anchors under them are merged into services
# by the YAML loader before Compose ever sees the document. A tool that strips x- keys BEFORE
# parsing YAML destroys the anchors and the file stops resolving.
x-logging: &default-logging
  driver: json-file
  options:
    max-size: 10m
    max-file: "3"

x-common: &common
  restart: unless-stopped
  logging: *default-logging
  networks:
    - backend
  environment:
    TZ: Etc/UTC

x-healthcheck-defaults: &healthcheck-defaults
  interval: 10s
  timeout: 2s
  retries: 3

services:
  api:
    <<: *common
    image: registry.example.invalid/example-org/orders:1.4.0
    ports:
      - "8080:8080"
    healthcheck:
      <<: *healthcheck-defaults
      test: ["CMD", "example-healthcheck"]

  worker:
    <<: *common
    image: registry.example.invalid/example-org/worker:1.4.0
    command: ["worker", "--queue", "orders"]
    healthcheck:
      <<: *healthcheck-defaults
      retries: 10

networks:
  backend:
    driver: bridge

Specifications

Anchors
3
Merge Keys
4
Extension Fields
3
Services
2
Edge Case
stripping x- keys before YAML parsing destroys the anchors

Testing contract

Expected to pass
Scenario
Resolve a Compose file whose service definitions come from anchors declared under x- keys
Expected result
Both services inherit restart, logging and networks from the merged anchors, and worker's local retries value of 10 overrides the merged default of 3

What is a .yaml file?

YAML (YAML Ain't Markup Language) is a human-readable data-serialization format using indentation, key-value pairs, and lists, and is a superset of JSON. It supports comments, anchors, and multiple documents per file, favoring readability for configuration. Its indentation sensitivity makes it error-prone to hand-edit.

How to use this file

Use an example YAML file to test config parsers, indentation and anchor handling, multi-document streams, and safe-loading to avoid arbitrary object construction.

How to use this file for testing

“Docker Compose Anchors and x- Extension Fields” is a deterministic Novus Examples fixture for Config parsing, Encoding detection, Editor testing. TOML and INI configuration files with nested sections and typed values — for testing config parsers and loaders.

Documented properties for this file: YAML · 1,125 bytes. Compare results against paired or grouped companions on this page when present (clean↔damaged, searchable↔scanned, or format twins) so scores stay reproducible across runs.

Download the file once, keep the path stable in CI or local scripts, and treat the spec table as the contract: dimensions, seeds, field lists, and roles are intentional. Corrupt or invalid samples are labelled as such — expect parsers to fail loudly rather than silently accept them.

Pipeline and infrastructure fixtures are inert configuration: steps reference fictional images and scripts, and nothing here executes. Run your linter, schema validator, migrator, or policy engine against them, and expect the deprecated-syntax and intentionally invalid variants to be rejected.

Point your config loader at the file and assert it reads the documented sections and typed values, including any deliberately-tricky nesting or comments.

Code examples

import yaml  # pip install pyyaml

with open("compose-anchors-extensions.yaml") as f:
    data = yaml.safe_load(f)
print(data)

Generated by generation/pipelines.py. Free for any use, no attribution required — license.