What is Docker Compose?

Docker Compose defines and runs multi-container applications from a single YAML file with one command.

Real applications are made of several containers — a web server, an API, a database, a cache. Running and wiring them up by hand is tedious. Docker Compose solves this by describing your whole stack in one file.

How it works

You write a compose.yml file listing each container as a service. Then docker compose up starts everything at once, creating the networks and volumes automatically.

A single Compose file starting multiple connected servicescompose.ymldocker compose upweb (nginx)api (node)db (postgres)
One compose.yml describes every service; a single command brings the whole stack up.

Why it is great

  • The entire environment is version-controlled in one file.
  • New teammates run one command to start everything.
  • Services get a shared network and can reach each other by name.

Example

Example · bash
# Start the whole stack in the background
docker compose up -d

# See running services
docker compose ps

# Stop and remove everything
docker compose down

When to use it

  • A developer replaces three separate docker run commands with a single docker compose up to start a web app, database, and cache together.
  • A team commits a compose.yml to the repo so any new developer can reproduce the full local environment with one command.
  • A QA engineer uses docker compose up to spin up an isolated copy of the entire application stack for each test suite run.

More examples

Start all services

Starts every service defined in compose.yml in the background and then lists their status.

Example · bash
# Start all services in detached mode
docker compose up -d

# View running services
docker compose ps

Stop and remove all services

Tears down the whole stack; -v also deletes named volumes declared in the compose file.

Example · bash
# Stop and remove containers, networks
docker compose down

# Also remove volumes
docker compose down -v

View logs across all services

Streams interleaved log output from all services, colour-coded by service name.

Example · bash
# Follow logs from all services
docker compose logs -f

# Logs from a single service only
docker compose logs -f api

Discussion

  • Be the first to comment on this lesson.