Docker Compose: Run Many Containers Together
How to describe a small stack (app + database + cache) in one YAML file and start it all with a single command.
Series
Docker from Scratch
Docker Compose
Real apps are rarely just one container. You usually need:
- your app,
- a database,
- maybe a cache (Redis),
- maybe a queue.
Docker Compose lets you describe all of them in one docker-compose.yml file and start them together.
A tiny working example
services:
web:
build: .
ports:
- "3000:3000"
environment:
DATABASE_URL: postgres://user:pass@db:5432/app
depends_on:
- db
db:
image: postgres:16-alpine
environment:
POSTGRES_USER: user
POSTGRES_PASSWORD: pass
POSTGRES_DB: app
volumes:
- db-data:/var/lib/postgresql/data
volumes:
db-data:
Two commands to remember
docker compose up -d # start everything in the background
docker compose down # stop and remove everything
Beginner tips
- Service names become hostnames: from
web, connect to the DB usingdb:5432— notlocalhost. volumeskeep your database data safe across restarts.depends_onstarts services in order (but does not wait for them to be ready; use a healthcheck for that).
Real-world example
For a college project you can spin up a Node.js app, a Postgres database and a Redis cache with one command, then wipe everything with one command — perfect for demos and testing.
Keep reading
You may also like
devops
Git Handbook – Quick Reference
A complete collection of Git commands and notes, covering setup, commits, branching, merging, conflicts, and advanced workflows. This handbook is designed as a simple, step‑by‑step guide for beginners and professionals to quickly learn and apply Git in real projects.
devops
Kubernetes Explained for Absolute Beginners
The clearest possible introduction to Kubernetes — what it is, why it exists, and the seven words you need to know.
devops
GitHub Actions: A Complete CI Pipeline
Lint, test, build, publish — a battle-tested workflow template.
Discussion (0)
No comments yet. Be the first to weigh in.