What is Docker Compose and when should you use it?
Docker Compose defines and runs multi-container apps from one YAML file. Learn how it works, when to use it, key commands, and examples for dev and testing.
Expected Interview Answer
Docker Compose is a tool for defining and running multi-container applications using a single declarative YAML file, so you can start an entire stack of related services with one command instead of managing each container by hand.
In a compose.yaml file you declare each service (web, database, cache), its image or build context, ports, volumes, environment variables, and dependencies. Running docker compose up then creates a shared network, starts every service, and wires them together so they can reach each other by service name. It is ideal for local development and testing of multi-service apps and for simple single-host deployments, but for production clustering across many machines you typically move to an orchestrator like Kubernetes or Docker Swarm.
- Defines a whole multi-container stack in one file
- Starts and stops all services with a single command
- Automatic shared network and service-name DNS
- Reproducible environments across the team
- Version-controllable infrastructure definition
- Easy to add volumes, env vars, and dependencies
AI Mentor Explanation
Running containers by hand is calling each fielder onto the ground one at a time and pointing to their position individually. Docker Compose is the captain's field-setting sheet: one plan that places bowler, keeper, and slips all at once, each knowing its spot and how it relates to the others, deployed the moment the over begins.
Step-by-Step Explanation
Step 1
Write compose.yaml
Declare each service with its image or build context, ports, volumes, and environment variables.
Step 2
Define dependencies
Use depends_on so, for example, the web service waits for the database to be created first.
Step 3
Bring the stack up
Run docker compose up -d to create a shared network and start every service in the background.
Step 4
Use service-name networking
Services reach each other by name (e.g. the app connects to host 'db'), no manual IPs needed.
Step 5
Inspect and iterate
Use docker compose ps and logs to check status; edit the file and re-run up to apply changes.
Step 6
Tear it down
docker compose down stops and removes the containers and network, optionally with -v to drop volumes.
What Interviewer Expects
- Definition as a tool for multi-container apps via YAML
- Awareness of the single-command up/down workflow
- Understanding of automatic shared networking and service DNS
- Knowing depends_on, volumes, and environment configuration
- Recognizing it suits dev/test and single-host, not large-scale clustering
- Contrast with Kubernetes/Swarm for production orchestration
Common Mistakes
- Confusing Docker Compose with Kubernetes-style multi-host orchestration
- Thinking depends_on waits for a service to be fully ready (it only waits for start)
- Hardcoding container IPs instead of using service names
- Believing Compose is required to run a single container
- Forgetting that docker compose down -v deletes named volumes and data
Best Answer (HR Friendly)
“Docker Compose lets you describe an app made of several containers — like a website plus its database — in one file and start them all together with a single command. You use it mainly for local development, testing, and simple deployments where you want the whole stack running consistently without setting up each piece manually.”
Code Example
services:
web:
build: .
ports:
- "3000:3000"
environment:
DATABASE_URL: postgres://app:secret@db:5432/appdb
depends_on:
- db
db:
image: postgres:16-alpine
environment:
POSTGRES_USER: app
POSTGRES_PASSWORD: secret
POSTGRES_DB: appdb
volumes:
- db-data:/var/lib/postgresql/data
volumes:
db-data:# start the whole stack in the background
docker compose up -d
# view service status and logs
docker compose ps
docker compose logs -f web
# stop and remove containers + network (keep volumes)
docker compose down
# also remove named volumes (deletes data)
docker compose down -vFollow-up Questions
- How does depends_on differ from actually waiting for a service to be healthy?
- How do services in a Compose file discover and talk to each other?
- When would you outgrow Docker Compose and move to Kubernetes?
- How do named volumes in Compose persist data across restarts?
- How can you override settings for different environments with multiple compose files?
MCQ Practice
1. What is the primary purpose of Docker Compose?
Compose declares a multi-service app in one file and manages its lifecycle with single commands like up and down.
2. How do services in a Compose file typically reach each other?
Compose creates a shared network and provides service-name DNS, so a service connects to another using its name as the host.
3. What does depends_on guarantee for a dependent service?
By default depends_on only controls start order; it does not wait for the dependency to be healthy or ready without extra health-check conditions.
Flash Cards
Docker Compose — A tool to define and run multi-container apps from one declarative YAML file.
Key command to start a stack — docker compose up -d — starts all services on a shared network.
How do services talk? — By service name over the auto-created shared network.
depends_on limitation — Only controls start order, not readiness/health.
When NOT to use it — Large-scale multi-host production clustering — use Kubernetes or Swarm.