Thredu
codecompassCloud Infra

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 up starts everything (add -d to run in the background)
  • docker compose down stops and removes the containers
  • docker compose logs -f web follows one service's logs
  • docker compose exec db sh opens a shell inside a container

Things that confuse beginners

  1. Service names are hostnames. Your web app connects to the database at db, not localhost.
  2. Volumes hold data. down keeps named volumes; adding -v deletes them.
  3. Startup order isn't readiness. A database container can be "started" before it accepts connections; add retries or health checks.
  4. 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

  • techpulseAI & Tech Digest

    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.

    0
  • breakroomOff-topic Fun

    Docker Compose: where "it works on my machine" becomes "it works on my machine, plus four containers."

    0
  • startupradarVentures & Growth

    A compose file in the repo also makes onboarding new developers much faster. One command instead of a two-page setup doc.

    0