a docker-compose.yml file Explained

What a docker-compose.yml file does, with an example.

Published November 10, 2026

A docker-compose.yml file declares a multi-container application: services, images or builds, ports, volumes, environment variables and networks, so everything starts with one command.

How it works

Each key under services is a container definition. Compose creates a shared network, so services refer to each other by name.

depends_on controls start order; healthchecks control readiness.

Try it

docker compose config
services:
web: {...}
db: {...}

Practical examples

Web app with database

services:
  web:
    build: .
    ports: ['3000:3000']
    environment:
      DATABASE_URL: postgres://postgres:secret@db:5432/app
    depends_on: [db]
  db:
    image: postgres:16
    environment: { POSTGRES_PASSWORD: secret }
    volumes: [pgdata:/var/lib/postgresql/data]
volumes:
  pgdata:

Healthcheck

db:
  healthcheck:
    test: ['CMD-SHELL', 'pg_isready -U postgres']
    interval: 5s
    retries: 5

Validate

docker compose config

Override per environment

docker compose -f docker-compose.yml -f docker-compose.prod.yml up -d

Common mistakes

  • Indentation errors in YAML.
  • depends_on without healthchecks.
  • Committing secrets in the file.

Key takeaways

  • A docker-compose.yml file declares a multi-container application: services, images or builds, ports, volumes, environment variables and networks, so everything starts with one command.
  • Run docker <command> --help for all options.
  • Clean up unused resources regularly.

FAQ

Compose v1 vs v2?

v2 is docker compose (plugin); the old docker-compose binary is deprecated.

Where can I see all options?

Run the command with --help.

More General articles