Interview's MateDocker
Interview's Mate

Docker

Compose · Networking · Security · Ops

Visualizer

DSA Visualizer
All 32 lessons
Custom Playground
Try your own examples

Learning

System Design
31 architecture topics
SQL Learning
160 foundations → production topics
AI Engineering
160 LLM → backend topics
Data Science
160 Python → MLOps topics
Data Engineer
160 SQL → streaming topics
Docker
160 Docker learning topics
Kubernetes
160 K8s fundamentals → cloud
Java LearnSoon
Coming soon
Python Learn
Interview Q&A & internals
JavaScript Learn
Closures, async & DOM Q&A
Tech Learn
Search, practise & track Q&A
AI DSA Practice
Solve coding problems
Theme
Home
Docker topics
Lesson 92/ 160
05. Docker Compose & Multi-Container Apps

Extending Services with extends, YAML Anchors & include

Reuse service definitions with extends, anchors, and Compose include.

Advancedextends · Anchors3 quiz questions
Practice DevOps Q&A
In plain wordsDiagramTheoryStep by stepWhen to use itInterview cluesMistakes to avoidQuiz (3)Practice

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

Loading interactive 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.

Concrete extends setup

x-service: &common { restart: unless-stopped, environment: { LOG_LEVEL: info } }; services: { api: { <<: *common } }

Key takeaway: Prove Anchors with 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.

Deliberate failure test

Exercise this failure safely: assuming IDE anchor previews represent Compose merge semantics, or changing a shared anchor and failing to validate every included service.

Key takeaway: Reuse reduces drift; duplicated explicit models are easier for novices to audit when a field is security-sensitive.

How it works, step by step

Step 1 of 4
Design

1. 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.

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.
Key numbers at this step
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 environment in 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

Explicit extends
Best for: Reuse service definitions with extends, anchors, and Compose include, with a reviewed runbook.

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.

Ambient default
Best for: Short-lived Anchors experiments with low blast radius.

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.

Managed include
Best for: Teams that centralize include policy across many workloads.

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 skipping expand 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?
Interview tip: Give one concrete operation-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

Loading quiz

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.

Open DevOps Q&A →Tech Learn Q&A
Previous topic
Merging & Overriding Compose Files (compose.override.yaml, -f)
Next topic
Running One-Off Tasks & Migrations (docker compose run, exec)