Docker Compose
Define and run multi-container applications with a single Compose file.
Docker Compose describes a multi-container app in one docker-compose.yml and
runs it with a single command. Each service is a container; Compose also manages
networks and volumes for them.
Key features: services reach each other by service name on a shared network
(db:5432), depends_on orders startup, environment/.env injects config,
ports publish to the host, and volumes persist data. docker compose up
builds and starts everything; down cleans it up.
services:
web:
build: ./web
ports: ["8080:80"]
depends_on: [api]
api:
build: ./api
environment:
DATABASE_URL: postgres://db:5432/app
db:
image: postgres:16
volumes: ["dbdata:/var/lib/postgresql/data"]
volumes:
dbdata:docker compose up --build # start the whole stack
docker compose logs -f # tail combined logs
docker compose down # stop and remove - Write a Compose file with a web service that depends on an API service.
- Add a named volume to persist a database’s data.
- Explain how one service reaches another by name.
- Use
docker compose down -vand describe what-vremoves.
Cheat Sheet▾
| Task | Command / key |
|---|---|
| Start stack | docker compose up --build |
| Stop + remove | docker compose down |
| Logs | docker compose logs -f |
| List services | docker compose ps |
| Service-to-service | by service name |
| Persist data | named volumes: |
| Startup order | depends_on |
Common Interview Questions▾
How do services in Docker Compose communicate?
Over a shared Compose network, addressing each other by service name (Compose provides DNS), e.g. the api service reaches the database at db:5432.
What does depends_on do — and not do?
It controls start order so a service starts after its dependencies’ containers start. It does not wait for them to be fully ready; use health checks for that.