Extending Services with extends, YAML Anchors & include
Reuse service definitions with extends, anchors, and Compose include.
In plain words
Use anchors for simple in-file fields, extends for one controlled service baseline and include for a modular application only after testing resolved paths and variable scope.
Think of it like…
Anchors are footnotes on one sheet, extends is cloning one workstation diagram, and include is stapling another department’s blueprint into the same project binder.
Where you’ve seen it
In practice, x-service: &common { restart: unless-stopped, environment: { LOG_LEVEL: info } }; services: { api: { <<: *common } } is the concrete move for this job: reuse service definitions with extends, anchors, and Compose include. The release is not accepted until expand docker compose config and search the final model for the source/target paths plus duplicated environment confirms the result.
Words you’ll hear
- extends
- The implementation control in
x-service: &common { restart: unless-stopped, environment: { LOG_LEVEL: info } }; services: { api: { <<: *common } }for reuse service definitions with extends, anchors, and Compose include. - Anchors
- The result to observe with
expand docker compose config and search the final model for the source/target paths plus duplicated environment, rather than infer from a successful command alone. - include
- The production boundary exposed when assuming IDE anchor previews represent Compose merge semantics, or changing a shared anchor and failing to validate every included service.
Visual diagram
Theory & deep dive
extends control path
The concrete mechanism is x-service: &common { restart: unless-stopped, environment: { LOG_LEVEL: info } }; services: { api: { <<: *common } }. The observable result is expand docker compose config and search the final model for the source/target paths plus duplicated environment; if those disagree, inspect the runtime evidence before changing unrelated settings.
Reuse service definitions with extends, anchors, and Compose include. Keep extends, Anchors and include in the same deployment record so the intent and the observed state cannot drift apart.
x-service: &common { restart: unless-stopped, environment: { LOG_LEVEL: info } }; services: { api: { <<: *common } }
expand docker compose config and search the final model for the source/target paths plus duplicated environment, not with command success alone.include failure boundary
Anchors are parsed before Compose sees the model, can duplicate opaque structures, and do not cross files. extends and include can hide the source of a changed port or bind, and later relative paths resolve differently depending on the feature. The narrow failure to design for is assuming IDE anchor previews represent Compose merge semantics, or changing a shared anchor and failing to validate every included service.
Accept this compromise explicitly: Reuse reduces drift; duplicated explicit models are easier for novices to audit when a field is security-sensitive. The controlled check-expand docker compose config and search the final model for the source/target paths plus duplicated environment-also identifies the point where a different design is safer.
Exercise this failure safely: assuming IDE anchor previews represent Compose merge semantics, or changing a shared anchor and failing to validate every included service.
How it works, step by step
Step 1 of 41. State the boundary
Write down why the workload needs this: Anchors are parsed before Compose sees the model, can duplicate opaque structures, and do not cross files. extends and include can hide the source of a changed port or bind, and later relative paths resolve differently depending on the feature.
- Intent
- extends
When to use it & trade-offs
What it is
YAML anchors reuse nodes within one parsed YAML graph, extends imports service configuration from another Compose model, and include incorporates another Compose application with optional path resolution. These remove duplication at different scopes and have different merge/path semantics.
Why it matters: Anchors are parsed before Compose sees the model, can duplicate opaque structures, and do not cross files. extends and include can hide the source of a changed port or bind, and later relative paths resolve differently depending on the feature.
When to reach for it
- The fit is strongest for this job: reuse service definitions with extends, anchors, and Compose include. Pair it with
expand docker compose config and search the final model for the source/target paths plus duplicated environmentin the runbook. - Production extends changes should make Anchors observable through
x-service: &common { restart: unless-stopped, environment: { LOG_LEVEL: info } }; services: { api: { <<: *common } }, rather than trusting the runtime default. - Keep a rollback example for the case where assuming IDE anchor previews represent Compose merge semantics, or changing a shared anchor and failing to validate every included service.
When to avoid or be careful
- Do not accept command success while the system can still fail by assuming IDE anchor previews represent Compose merge semantics, or changing a shared anchor and failing to validate every included service.
- Avoid erasing include protection merely to dodge this cost: “Reuse reduces drift; duplicated explicit models are easier for novices to audit when a field is security-sensitive”.
- Do not replace the evidence path-
expand docker compose config and search the final model for the source/target paths plus duplicated environment-with a screenshot or a mutable tag.
x-service: &common { restart: unless-stopped, environment: { LOG_LEVEL: info } }; services: { api: { <<: *common } }: practical choices
Strengths: YAML anchors reuse nodes within one parsed YAML graph, extends imports service configuration from another Compose model, and include incorporates another Compose application with optional path resolution. These remove duplication at different scopes and have different merge/path semantics.
Watch out: Reuse reduces drift; duplicated explicit models are easier for novices to audit when a field is security-sensitive.
Strengths: It can begin with x-service: &common { restart: unless-stopped, environment: { LOG_LEVEL: info } }; services: { api: { <<: *common } } and fewer surrounding controls.
Watch out: It silently accepts assuming IDE anchor previews represent Compose merge semantics, or changing a shared anchor and failing to validate every included service, which makes the result hard to reproduce after promotion.
Strengths: Anchors are parsed before Compose sees the model, can duplicate opaque structures, and do not cross files. extends and include can hide the source of a changed port or bind, and later relative paths resolve differently depending on the feature.
Watch out: The platform adds an ownership and debugging layer; retain expand docker compose config and search the final model for the source/target paths plus duplicated environment as the direct check.
Interview signals & numbers to know
When this topic comes up
- x-service: &common { restart: unless-stopped, environment: { LOG_LEVEL: info } }; services: { api: { <<: *common } }
- expand docker compose config and search the final model for the source/target paths plus duplicated environment
- Failure signature: assuming IDE anchor previews represent Compose merge semantics, or changing a shared anchor and failing to validate every included service
- Trade-off: Reuse reduces drift; duplicated explicit models are easier for novices to audit when a field is security-sensitive.
Benchmarks & rules of thumb
- 3 reuse tools
- anchor, extends and include
- 1 YAML graph
- scope of an ordinary anchor
- 0 implicit path clarity
- without inspecting normalized Compose config
Common mistakes & follow-ups
Common interview mistakes
- Treating a successful
x-service: &common { restart: unless-stopped, environment: { LOG_LEVEL: info } }; services: { api: { <<: *common } }as proof and skippingexpand docker compose config and search the final model for the source/target paths plus duplicated environment. - Assuming IDE anchor previews represent Compose merge semantics, or changing a shared anchor and failing to validate every included service
- Optimizing extends without accepting that reuse reduces drift; duplicated explicit models are easier for novices to audit when a field is security-sensitive.
Likely interviewer follow-ups
- Why does
x-service: &common { restart: unless-stopped, environment: { LOG_LEVEL: info } }; services: { api: { <<: *common } }address extends here rather than through application code? - How would you distinguish healthy Anchors from the failure “assuming IDE anchor previews represent Compose merge semantics, or changing a shared anchor and failing to validate every included service” using
expand docker compose config and search the final model for the source/target paths plus duplicated environment? - When would this trade-off-“Reuse reduces drift; duplicated explicit models are easier for novices to audit when a field is security-sensitive”-justify a different include design?
x-service: &common { restart: unless-stopped, environment: { LOG_LEVEL: info } }; services: { api: { <<: *common } }-and one proof-expand docker compose config and search the final model for the source/target paths plus duplicated environment. Then name the boundary: assuming IDE anchor previews represent Compose merge semantics, or changing a shared anchor and failing to validate every included service.Knowledge check
Put it into practice
Practice related interview Q&A
Apply Extending Services with extends, YAML Anchors & include with generative AI interview Q&A and hands-on practice.