Docker Compose in 5 minutes

·cheatly.dev

Docker Compose lets you define multiple containers in one file and start them all with a single command. Instead of running five docker run commands with twenty flags each, you write a docker-compose.yml once and run docker compose up.

A real example

Say you need a Node.js app with a PostgreSQL database. Create docker-compose.yml:

services:
  app:
    build: .
    ports:
      - "3000:3000"
    environment:
      - DATABASE_URL=postgres://user:pass@db:5432/myapp
    depends_on:
      - db

  db:
    image: postgres:16
    environment:
      - POSTGRES_USER=user
      - POSTGRES_PASSWORD=pass
      - POSTGRES_DB=myapp
    volumes:
      - pgdata:/var/lib/postgresql/data

volumes:
  pgdata:

That's it. Two services, connected automatically.

The commands you need

Daily Compose Commands
docker compose up
Start all services (add -d for background)
docker compose down
Stop and remove all containers
docker compose logs -f
Follow logs from all services
docker compose ps
Show running containers
docker compose exec app sh
Open a shell inside a running container
docker compose build
Rebuild images (after changing Dockerfile)

Key concepts in 30 seconds

Services are containers. Each one gets a name (like app and db above) that doubles as a hostname on the internal network. Your app connects to db:5432 — Docker resolves the name automatically.

Ports map container ports to your machine. "3000:3000" means localhost:3000 reaches the container's port 3000.

Volumes persist data. Without the pgdata volume above, your database would lose everything when the container stops.

depends_on controls startup order. The app waits for the db container to start (but not for PostgreSQL to be ready — that's a different problem).

Use docker compose up --build to rebuild images and start in one command. Saves you from forgetting to rebuild after changing your Dockerfile.
Add docker-compose.yml to your repo. It's documentation and infrastructure in one file — anyone can clone your project and run it with a single command.

For the full reference, see our Docker cheatsheet.