Docker Compose

Database + API + cache, one command, one circus.

▶ the lesson · narrated sketch video · tap for sound

the intro

Compose runs multi-container apps from one YAML file: your API, its database, a cache, all networked together. One command starts everything in the right order. It is the local development standard for anything bigger than a single service.

fun fact ✦

Compose file names have a poetry to them: db, api, web — a whole circus in 30 lines of YAML.

the docs

the deep dive

the ideas behind the video — concepts, diagrams, a walkthrough and the traps to avoid

A docker run command is a story you tell once and forget by Friday. Compose is the same story written down: every service, port, and volume as reviewable YAML, so the whole stack comes up the same way on every laptop. The detail that makes it feel magic: services reach each other by name, like coworkers in one office.

the runbook as code, not folklore

compose.yamlapipostgresredis
one file declares the herd; up starts them all on a shared network

key terms

service

One entry in the YAML: an image or build plus ports, env, and volumes. Up runs it as containers.

api, db, redis, worker

shared network

Compose creates one network where every service name is a working hostname. No IPs, no internal port mapping.

connect to db:5432

named volume

Docker-managed storage that survives down and rebuilds. Database data lives here.

pgdata:/var/lib/postgresql/data

depends_on

Start ordering. Plain ordering only waits for start; service_healthy waits until a healthcheck passes.

condition: service_healthy

how it works

  1. 1
    Describe each service

    One YAML entry per container: image or build, ports, env, volumes. Your whole stack fits on half a page.

  2. 2
    Run docker compose up

    Compose builds what needs building, creates the shared network, and starts everything — waiting on healthchecks if you asked.

  3. 3
    Talk by service name

    Inside the network the api connects to host db on port 5432. Service names are real hostnames; nothing to track.

  4. 4
    Tear down with down

    docker compose down stops and removes containers and networks. Named volumes survive, so your database does not forget.

the code, explained

the whole stack, in eight lines

1services:
2 api:
3 build: .
your API image, built from the local Dockerfile
4 db:
5 image: postgres:16-alpine
6 volumes: ["pg:/var/lib/postgresql/data"]
named volume: data survives docker compose down
7volumes:
8 pg:

side by side

hand-typed runs

  • retyped from memory each time
  • network flags easy to fumble
  • one typo and stacks differ
  • no single source of truth
VS

compose.yaml

  • stack defined as code
  • reviewable and shareable
  • names wired as hostnames
  • up and down run the herd
🪤 gotcha

Changed the Dockerfile? docker compose up happily reuses the stale image and runs yesterday's code. Use docker compose up --build, or set up develop: watch — a large share of compose debugging sessions are a stale image wearing a trenchcoat.

quick answers

Is Compose ready for production?

It is built for local development and single-host deploys. Fine for a small VPS; true multi-host orchestration — scaling, self-healing, rolling updates — is Kubernetes territory.

How do services find each other?

Compose puts them on one private network where each service name resolves as a hostname. Your API connects to db:5432 and DNS just works.

Where do environment variables come from?

A .env file next to compose.yaml is loaded automatically. Real secrets stay out of git exactly like before — Compose changes the plumbing, not the rules.

quick hits about Docker Compose

short answers · interviews, tips, trivia & hidden gems

🎤 interview

Why Compose?watch ↗

📚 tutorial

How do services talk?watch ↗

🔧 useful

Secrets in Compose?watch ↗

💎 deep

Start order done rightwatch ↗

💎 deep

Start half the stackwatch ↗

🆕 update

Compose Watchwatch ↗