C.W.K.
Stream
Lesson 02 of 04 · published

Why Compose — One Command, Whole Stack

~12 min · compose

Level 0Container Curious
0 XP0/36 lessons0/10 achievements
0/120 XP to next level120 XP to go0% complete

Compose replaces shell scripts that string together docker run commands

A four-service local stack — API, web, Postgres, Redis — with all the right networks, volumes, env vars, and start order takes about 8 lines of bash and is a nightmare to maintain. Compose puts the same stack in a YAML file and brings it up with docker compose up -d.

What Compose buys you

  • Declarative — the file is the truth, not your shell history.
  • Networks created for free — every service joins the project's default network with name-based DNS.
  • One command for up, down, logs, exec, scale.
  • Override files for dev vs prod without copy-paste.

Code

Without Compose (the pain)·bash
docker network create app-net
docker volume create pgdata

docker run -d --name db --network app-net \
  -v pgdata:/var/lib/postgresql/data \
  -e POSTGRES_PASSWORD=secret postgres:16

docker run -d --name redis --network app-net redis:7-alpine

docker run -d --name api --network app-net -p 8000:8000 \
  -e DATABASE_URL=postgres://postgres:secret@db:5432/app \
  -e REDIS_URL=redis://redis:6379 \
  myapi:1.0

docker run -d --name web --network app-net -p 3000:3000 myweb:1.0
With Compose — same thing·yaml
services:
  db:
    image: postgres:16
    volumes: [pgdata:/var/lib/postgresql/data]
    environment:
      POSTGRES_PASSWORD: secret
  redis:
    image: redis:7-alpine
  api:
    build: ./api
    ports: ["8000:8000"]
    environment:
      DATABASE_URL: postgres://postgres:secret@db:5432/app
      REDIS_URL: redis://redis:6379
    depends_on: [db, redis]
  web:
    build: ./web
    ports: ["3000:3000"]

volumes:
  pgdata:

External links

Exercise

Convert a multi-container local setup of yours (real or invented: API + DB + cache works fine) from a shell-script-of-docker-runs to a single compose.yaml. Bring it up with docker compose up -d, exec into each service to verify reachability, then docker compose down. Commit the compose.yaml.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.