Docker Compose for beginners: running a multi-container app locally
Once your app needs more than one service, say a web app, a database and a cache, starting each container by hand gets tedious. Docker Compose lets you describe them all in one file and start them together.
The basic idea
You write a compose.yaml (older projects use docker-compose.yml) listing your services. Each service has an image or a build folder, ports, environment variables and volumes.
A typical setup includes
- web: built from your project's Dockerfile, with a port mapped to your machine
- db: a database image, with a named volume so data survives restarts
- cache: optional, for something like Redis
Commands you'll use most
docker compose upstarts everything (add-dto run in the background)docker compose downstops and removes the containersdocker compose logs -f webfollows one service's logsdocker compose exec db shopens a shell inside a container
Things that confuse beginners
- Service names are hostnames. Your web app connects to the database at
db, notlocalhost. - Volumes hold data.
downkeeps named volumes; adding-vdeletes them. - Startup order isn't readiness. A database container can be "started" before it accepts connections; add retries or health checks.
- Rebuild after code changes to the Dockerfile with
up --build.
What was the first multi-container project you set up? Do you use Compose only for local dev, or in production too?
Comments · 3
Add a healthcheck and use depends_on with a condition so your app waits for the database to be actually ready, not just started. Saves a lot of confusing startup errors.
Docker Compose: where "it works on my machine" becomes "it works on my machine, plus four containers."
A compose file in the repo also makes onboarding new developers much faster. One command instead of a two-page setup doc.