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 82/ 160
05. Docker Compose & Multi-Container Apps

Compose Specification & compose.yaml Structure

Learn the Compose Spec fields and modern compose.yaml layout.

Beginnercompose.yaml · Spec3 quiz questions
Practice DevOps Q&A
In plain wordsDiagramTheoryStep by stepWhen to use itInterview cluesMistakes to avoidQuiz (3)Practice

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

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

Concrete compose.yaml setup

services: { api: { build: ., environment: { LOG_LEVEL: info }, networks: [edge] } }; networks: { edge: {} }

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

Deliberate failure test

Exercise this failure safely: inventing a top-level runtime flag, or placing secrets under a custom key that YAML accepts but Compose ignores.

Key takeaway: A concise spec is readable; a very large generated Compose config should be linted and broken into reviewed overrides or components.

How it works, step by step

Step 1 of 4
Design

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

Keep resource declarations separate from per-service runtime settings and use docker compose config as the syntax and merge truth.
Key numbers at this step
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.api in 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

Explicit compose.yaml
Best for: Learn the Compose Spec fields and modern compose.yaml layout, with a reviewed runbook.

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.

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

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.

Managed Top-Level Keys
Best for: Teams that centralize Top-Level Keys policy across many workloads.

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

Loading quiz

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.

Open DevOps Q&A →Tech Learn Q&A
Previous topic
Why Docker Compose? Declarative Multi-Container Orchestration
Next topic
Defining Services, Builds, Images & Container Names