✦ For everyone, free.

Practical knowledge for real and everyday life

Home

9 Docker Compose

A focused guide to Docker Compose, connecting core concepts with practical Docker and container operations.

Docker Compose is a tool for defining and running multi-container applications using a single declarative configuration file, replacing what would otherwise require numerous individual docker run commands with a unified, version-controllable definition of an entire application's services, networks, and volumes.

Defining a Multi-Container Application

A Compose file declares each service an application consists of, along with their configuration, in a single, human-readable YAML document.

services:
  api:
    build: .
    ports:
      - "8080:8080"
    environment:
      - DATABASE_URL=postgres://db:5432/app
  db:
    image: postgres:16
    volumes:
      - pgdata:/var/lib/postgresql/data

volumes:
  pgdata:
Starting an Entire Application With a Single Command

Rather than running multiple separate docker run commands, Compose starts every defined service together, correctly networked and configured.

docker compose up -d
Automatic Networking and Service Discovery

Compose automatically creates a dedicated network for the application and makes each service resolvable by its defined name, without requiring any manual network setup.

docker compose exec api ping db
Stopping and Cleaning Up the Entire Application

The entire multi-container application can be stopped and removed together, just as easily as it was started.

docker compose down
Why Docker Compose Matters

Compose dramatically simplifies the definition, sharing, and operation of multi-container applications, replacing error-prone, manually issued sequences of individual commands with a single, declarative, version-controllable file that accurately and consistently describes how an entire application's components fit together.

Content in this section