What is Docker Compose and how does it run multi-container apps?
Take a moment.
Say your answer out loud or write it down first — retrieval is what makes it stick. Reveal when you're ready.
Short answer
Compose is a declarative client: you describe every service, port, volume, and network in one compose.yaml, and docker compose up -d --build reconciles everything (pulls, networks, volumes, containers) while docker compose down tears it down. One container per concern — e.g. Node app + MySQL — wired automatically.
Detailed explanation
Multiple docker run commands plus manual networks/flags/cleanup collapse under real stacks (frontend + API + db + queue). Compose fixes it: commit a compose.yaml defining services with image/ports/volumes/networks, and anyone clones and runs one command. The docs' todo-list app (Node + MySQL) shows the payoff: docker compose up -d --build pulls node and MySQL, creates network todo-list-app_default and volume todo-mysql-data, and starts both containers — open http://localhost:3000 after ~10-15s. Compose is reconciling, not scripting: edit the file and up again applies just the delta. Tear down with docker compose down (removes containers + network, keeps volumes so data survives a restart) or down --volumes to delete data too; Desktop's Delete on a stack removes containers only. Remember the separation: Dockerfile builds an image, Compose defines running containers (often referencing a Dockerfile to build a service).
Official docs:Docker docs: What is Docker Compose?
Example
# compose.yaml — same welcome app as `docker run -d -p 8080:80 …`
services:
app:
image: docker/welcome-to-docker
ports:
- 8080:80
# multi-service shape (todo app: Node + MySQL):
# services:
# app: { build: ., ports: ["3000:3000"], depends_on: [mysql] }
# mysql: { image: mysql:8, volumes: [todo-mysql-data:/var/lib/mysql] }
docker compose up -d --build # create network + volume + containers
docker compose down # remove containers + network (keep volumes)
docker compose down --volumes # also delete the dataInterview tip
Contrast Dockerfile (builds one image) vs Compose file (runs many containers) in one line, then note volumes survive down unless --volumes — the two facts interviewers probe most.
Common mistakes
- Stuffing database + backend + frontend into one container instead of one service each.
- Assuming
compose downdeletes data (it keeps volumes by default).