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.
# 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 Testaroo 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.
Related files
- ymlGitHub Actions on: Key YAML Boolean TrapThe workflow file that exposes the YAML 1.1 boolean resolver: a bare `on` key becomes True in PyYAML, SnakeYAML and Psych, so a round-trip through those loaders loses the trigger block. The env values repeat the trap with NO, yes and off alongside a quoted control.

- ymlGitLab CI !reference Custom YAML TagGitLab's `!reference` custom YAML tag used three ways: inside a script list, as a whole after_script value, and as a whole rules value. A stock YAML 1.2 loader has no constructor for it and raises; GitLab's loader resolves each key path.

- ymlGitLab CI Anchors and ExtendsThe two GitLab inheritance mechanisms in one file: YAML anchors with merge keys, which the loader resolves before GitLab sees the document, and `extends`, which GitLab resolves afterwards with a deep merge. Hidden .job keys carry both.

- yamlYAML Anchors and AliasesAnchors and aliases across mapping, sequence and scalar nodes, including an anchor nested inside another anchored node. The point most tooling misses: an alias is a reference to the same node, so a shallow-loading parser can share mutable state between jobs.

- yamlYAML Block Scalars and Chomping IndicatorsEvery block-scalar variation in one file: literal and folded styles, all three chomping indicators, an explicit indentation indicator, a folded block with a paragraph break, and an embedded shell-shaped script whose own indentation must survive.

- yamlYAML Bounded Anchor ReuseNested anchor reuse kept deliberately small: two anchors, six references, thirty-one expanded nodes. It is here to test alias resolution and expansion accounting on a safe input, and is explicitly not a billion-laughs expansion bomb.
