Compose Specification & compose.yaml Structure
Learn the Compose Spec fields and modern compose.yaml layout.
In plain words
Keep resource declarations separate from per-service runtime settings and use docker compose config as the syntax and merge truth.
Think of it like…
The top-level headings are the departments in a production blueprint, while service blocks list where a team members each machine and doorway.
Where you’ve seen it
In practice, services: { api: { build: ., environment: { LOG_LEVEL: info }, networks: [edge] } }; networks: { edge: {} } is the concrete move for this job: learn the Compose Spec fields and modern compose.yaml layout. The release is not accepted until docker compose config --quiet and docker compose config --format json | jq .services.api confirms the result.
Words you’ll hear
- compose.yaml
- The implementation control in
services: { api: { build: ., environment: { LOG_LEVEL: info }, networks: [edge] } }; networks: { edge: {} }for learn the Compose Spec fields and modern compose.yaml layout. - Spec
- The result to observe with
docker compose config --quiet and docker compose config --format json | jq .services.api, rather than infer from a successful command alone. - Top-Level Keys
- The production boundary exposed when inventing a top-level runtime flag, or placing secrets under a custom key that YAML accepts but Compose ignores.
Visual diagram
Theory & deep dive
compose.yaml control path
The concrete mechanism is services: { api: { build: ., environment: { LOG_LEVEL: info }, networks: [edge] } }; networks: { edge: {} }. The observable result is docker compose config --quiet and docker compose config --format json | jq .services.api; if those disagree, inspect the runtime evidence before changing unrelated settings.
Learn the Compose Spec fields and modern compose.yaml layout. Keep compose.yaml, Spec and Top-Level Keys in the same deployment record so the intent and the observed state cannot drift apart.
services: { api: { build: ., environment: { LOG_LEVEL: info }, networks: [edge] } }; networks: { edge: {} }
docker compose config --quiet and docker compose config --format json | jq .services.api, not with command success alone.Top-Level Keys failure boundary
Indentation and extension keys can be valid YAML but invalid Compose fields. Top-level volumes: merely declares a reusable resource; a service must still mount it, and an unreferenced networks: declaration creates no active topology for that service. The narrow failure to design for is inventing a top-level runtime flag, or placing secrets under a custom key that YAML accepts but Compose ignores.
Accept this compromise explicitly: A concise spec is readable; a very large generated Compose config should be linted and broken into reviewed overrides or components. The controlled check-docker compose config --quiet and docker compose config --format json | jq .services.api-also identifies the point where a different design is safer.
Exercise this failure safely: inventing a top-level runtime flag, or placing secrets under a custom key that YAML accepts but Compose ignores.
How it works, step by step
Step 1 of 41. State the boundary
Write down why the workload needs this: Indentation and extension keys can be valid YAML but invalid Compose fields. Top-level volumes: merely declares a reusable resource; a service must still mount it, and an unreferenced networks: declaration creates no active topology for that service.
- Intent
- compose.yaml
When to use it & trade-offs
What it is
The Compose Specification organizes top-level services, networks, volumes, configs and secrets; service keys such as image/build, command, environment, ports, depends_on, deploy and healthcheck describe one runnable unit. compose.yaml is interpreted as a model, not a generic arbitrary YAML document.
Why it matters: Indentation and extension keys can be valid YAML but invalid Compose fields. Top-level volumes: merely declares a reusable resource; a service must still mount it, and an unreferenced networks: declaration creates no active topology for that service.
When to reach for it
- The fit is strongest for this job: learn the Compose Spec fields and modern compose.yaml layout. Pair it with
docker compose config --quiet and docker compose config --format json | jq .services.apiin the runbook. - Production compose.yaml changes should make Spec observable through
services: { api: { build: ., environment: { LOG_LEVEL: info }, networks: [edge] } }; networks: { edge: {} }, rather than trusting the runtime default. - Keep a rollback example for the case where inventing a top-level runtime flag, or placing secrets under a custom key that YAML accepts but Compose ignores.
When to avoid or be careful
- Do not accept command success while the system can still fail by inventing a top-level runtime flag, or placing secrets under a custom key that YAML accepts but Compose ignores.
- Avoid erasing Top-Level Keys protection merely to dodge this cost: “A concise spec is readable; a very large generated Compose config should be linted and broken into reviewed overrides or components”.
- Do not replace the evidence path-
docker compose config --quiet and docker compose config --format json | jq .services.api-with a screenshot or a mutable tag.
services: { api: { build: ., environment: { LOG_LEVEL: info }, networks: [edge] } }; networks: { edge: {} }: practical choices
Strengths: The Compose Specification organizes top-level services, networks, volumes, configs and secrets; service keys such as image/build, command, environment, ports, depends_on, deploy and healthcheck describe one runnable unit. compose.yaml is interpreted as a model, not a generic arbitrary YAML document.
Watch out: A concise spec is readable; a very large generated Compose config should be linted and broken into reviewed overrides or components.
Strengths: It can begin with services: { api: { build: ., environment: { LOG_LEVEL: info }, networks: [edge] } }; networks: { edge: {} } and fewer surrounding controls.
Watch out: It silently accepts inventing a top-level runtime flag, or placing secrets under a custom key that YAML accepts but Compose ignores, which makes the result hard to reproduce after promotion.
Strengths: Indentation and extension keys can be valid YAML but invalid Compose fields. Top-level volumes: merely declares a reusable resource; a service must still mount it, and an unreferenced networks: declaration creates no active topology for that service.
Watch out: The platform adds an ownership and debugging layer; retain docker compose config --quiet and docker compose config --format json | jq .services.api as the direct check.
Interview signals & numbers to know
When this topic comes up
- services: { api: { build: ., environment: { LOG_LEVEL: info }, networks: [edge] } }; networks: { edge: {} }
- docker compose config --quiet and docker compose config --format json | jq .services.api
- Failure signature: inventing a top-level runtime flag, or placing secrets under a custom key that YAML accepts but Compose ignores
- Trade-off: A concise spec is readable; a very large generated Compose config should be linted and broken into reviewed overrides or components.
Benchmarks & rules of thumb
- 5 standard top levels
- services, networks, volumes, configs and secrets
- 1 normalized config
- docker compose config reveals the merged result
- YAML indentation
- has semantic meaning and not just display formatting
Common mistakes & follow-ups
Common interview mistakes
- Treating a successful
services: { api: { build: ., environment: { LOG_LEVEL: info }, networks: [edge] } }; networks: { edge: {} }as proof and skippingdocker compose config --quiet and docker compose config --format json | jq .services.api. - Inventing a top-level runtime flag, or placing secrets under a custom key that YAML accepts but Compose ignores
- Optimizing compose.yaml without accepting that a concise spec is readable; a very large generated Compose config should be linted and broken into reviewed overrides or components.
Likely interviewer follow-ups
- Why does
services: { api: { build: ., environment: { LOG_LEVEL: info }, networks: [edge] } }; networks: { edge: {} }address compose.yaml here rather than through application code? - How would you distinguish healthy Spec from the failure “inventing a top-level runtime flag, or placing secrets under a custom key that YAML accepts but Compose ignores” using
docker compose config --quiet and docker compose config --format json | jq .services.api? - When would this trade-off-“A concise spec is readable; a very large generated Compose config should be linted and broken into reviewed overrides or components”-justify a different Top-Level Keys design?
services: { api: { build: ., environment: { LOG_LEVEL: info }, networks: [edge] } }; networks: { edge: {} }-and one proof-docker compose config --quiet and docker compose config --format json | jq .services.api. Then name the boundary: inventing a top-level runtime flag, or placing secrets under a custom key that YAML accepts but Compose ignores.Knowledge check
Put it into practice
Practice related interview Q&A
Apply Compose Specification & compose.yaml Structure with generative AI interview Q&A and hands-on practice.