Filters
How to search
  • Plain words find names, texts and examples.
  • "two words" finds the exact phrase.
  • --name or -x finds an option.
  • #word looks only inside commands.
  • @grep finds lessons and labs that use grep.
  • Filters: cat:network · in:labs · in:notes · level:rhcsa · ch:8 · root:yes. Put ! before a word to leave it out. OR between words means one of them.
  • ↑ ↓ to move · Enter to open · Esc to close · / to search

Chapter 8Name in the course: Docker Compose

Run multi-container apps with Compose

Compose describes a whole app — web, API, database, cache — in one compose.yaml file: images, ports, volumes, networks, variables and start order. One command starts or stops everything. It is perfect for development, tests and small single-host deployments.

  • 5 Lessons
  • 1 Commands

$ run as a normal user# run as root (or with sudo)

8.1

Write a compose file

Each service is a container; Compose creates a network where services find each other by name.

compose.yaml
services:
  web:
    image: nginx:1.27
    ports: ["8080:80"]
    volumes:
      - ./nginx.conf:/etc/nginx/conf.d/default.conf:ro
    depends_on:
      api:
        condition: service_healthy
  api:
    build: ./api
    environment:
      DATABASE_URL: postgres://shop:${DB_PASSWORD}@db:5432/shop
    healthcheck:
      test: ["CMD", "curl", "-fs", "http://localhost:8080/health"]
      interval: 10s
      retries: 5
    depends_on:
      db:
        condition: service_healthy
  db:
    image: postgres:16
    environment:
      POSTGRES_USER: shop
      POSTGRES_PASSWORD: ${DB_PASSWORD}
      POSTGRES_DB: shop
    volumes: [pgdata:/var/lib/postgresql/data]
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U shop"]
      interval: 5s
      retries: 10
volumes:
  pgdata:
.env
DB_PASSWORD=change-me-locally

8.2

Run the app

docker compose (or podman compose) works on the whole project.

docker compose up -d
docker compose ps
docker compose logs -f api
docker compose exec db psql -U shop
docker compose up -d --build api
docker compose pull && docker compose up -d
docker compose down
docker compose down -v
docker compose config
CommandDoes
up -dCreate and start everything in the background
up -d --buildRebuild images first
down / down -vStop and remove (and with -v delete volumes!)
configShow the final file with variables filled in

8.3

Wait for real readiness

depends_on alone only waits for the container to start. With condition: service_healthy, Compose waits for the health check to pass.

8.4

Use files per environment

A base file plus an override file per environment.

docker compose -f compose.yaml -f compose.prod.yaml up -d
docker compose --profile debug up -d
  • compose.override.yaml is read automatically in development.
  • Profiles start extra services only when asked (for example a debug tool).

Watch out

The .env file is for local values. Never commit real production secrets in it; use a secret store or Compose secrets:.

8.5

Know when to move on

Compose runs on one host. When you need many hosts, self-healing, rolling updates and autoscaling, move to Kubernetes — see CKA. Tools like Kompose can turn a compose file into Kubernetes manifests as a start.

All commands in this chapter