exit lab
DevOps learning path
IntermediateContainers

Docker Compose

Running the app, the database, and any supporting services together with one command.

1 hr

Overview

Docker Compose defines multiple containers — your app, a PostgreSQL database, maybe Redis — in one YAML file, and starts them all together as a coordinated stack with a single command.

Why it matters

A real backend service is never just the app — it needs a database at minimum. Compose means a new developer can clone the repo and run one command to get the app and database running together, instead of manually installing PostgreSQL locally and hoping the version matches.

How backend developers use it

A local dev docker-compose.yml with the Spring Boot service, a postgres service with a named volume for persistence, and environment variables wiring the app's datasource URL to the database container's service name (Compose gives each service a DNS name on its internal network, so the app connects to db:5432, not localhost).

Common mistakes

Warning

Forgetting depends_on, so the app container starts and tries to connect to a database that isn't ready yet — the fix is depends_on with a healthcheck condition, not a sleep hack.

Warning

Not using a named volume for the database, so all data is lost every time the container is recreated.

Warning

Connecting to localhost instead of the service name from inside another container — containers on the same Compose network resolve each other by service name, not localhost.

Warning

Committing a docker-compose.yml with real production credentials instead of reading them from a .env file that's gitignored.

Example commands

Start every service in the background

docker compose up -d

Rebuild images and restart

docker compose up -d --build

View logs from all services

docker compose logs -f

Stop and remove containers, keep volumes

docker compose down

Stop and remove containers and volumes (wipes the database)

docker compose down -v

Resources

Retrieval check

Before you continue

  • Explain what this tool or practice changes in the delivery lifecycle.
  • Name one common failure it helps you diagnose or prevent.
  • Repeat one example command from memory, then verify it.