| |

Docker 40 ๐Ÿณ Introduction to Docker Compose: Multi-Container Orchestration via Declarative YAML

โฑ๏ธ Estimated read: 18 min | |

Docker Compose begins with a single file. A compose.yaml file describes an entire application stack โ€” every service, network, volume, and configuration โ€” in declarative YAML. Instead of running docker run commands one by one, with all the flags, environment variables, port mappings, and network connections remembered separately, Compose reads the file and creates the whole stack from a single command: docker compose up. The file becomes the source of truth for how the application runs, and it can be version-controlled, shared, and reproduced on any machine that has Docker.

The shift from imperative commands to declarative configuration is the core idea. A docker run command is an instruction: “do this, then do that.” A compose.yaml file is a description: “this is what the application looks like.” Compose reconciles the actual state of the system with the desired state described in the file. If a service is missing, it creates it. If a network does not exist, it creates it. If the file changes, running docker compose up again recreates what changed. The declarative model means the file is not a script that executes once; it is a specification that Compose continuously enforces.

Compose is a single-host tool. It manages containers on one Docker host, and it is designed for development, testing, and small-scale production deployments. For multi-host orchestration, Docker Swarm or Kubernetes take over. But for the majority of development workflows โ€” a web app, an API, a database, a cache, a worker โ€” Compose is the standard tool. It is built into Docker Desktop and the Docker Engine CLI plugin, so it requires no separate installation .

This chapter covers three areas. First, why Compose exists โ€” the problem of managing multi-container applications with docker run, and the declarative model that solves it. Second, how the Compose application model works โ€” services, networks, volumes, and the top-level structure of compose.yaml. Third, how to run and manage a Compose stack โ€” the core lifecycle commands, the default network, service discovery by name, and the dependency ordering that prevents startup race conditions. The chapter ends with a complete example session, a quick reference, best practices, common pitfalls, real-world examples, and diagrams showing the Compose application model.

Key point: Docker Compose is a declarative tool for defining and running multi-container applications on a single host. A compose.yaml file describes services, networks, and volumes. docker compose up creates the entire stack, and Compose automatically creates a default network where services can reach each other by service name. The file is the source of truth, not a script that runs once.


Why Compose exists

The docker-run management problem. A multi-container application requires more than one container. A typical web application has a frontend, an API, a database, and a cache. Running each with docker run means remembering four separate commands, each with its own flags: port mappings, volume mounts, network connections, environment variables, and dependency order. If the application moves to a new machine, the commands must be reconstructed from memory or documentation. If a flag is forgotten, the application fails in a way that is difficult to diagnose. The commands are imperative โ€” they describe actions, not the desired end state โ€” and they do not compose .

The configuration-as-code problem. A compose.yaml file solves the management problem by moving the configuration out of shell history and into a version-controlled file. The file describes every service, every port, every volume, and every dependency. It can be reviewed in a pull request, diffed between versions, and shared with the team. A new developer clones the repository, runs docker compose up, and has the entire stack running. The setup is reproducible because it is not a sequence of commands; it is a specification .

The networking problem. Containers on the default Docker bridge network can reach each other by IP address, but the IP address changes when a container restarts. Hardcoding an IP address in application code is fragile. Compose solves this by creating a default network for the project and registering each service’s name as a DNS alias. A service named database can be reached at the hostname database from any other service in the same Compose project. Docker’s embedded DNS resolver handles the lookup and returns the current IP address . This means the application code does not need to know which container is running where; it uses the service name.

The dependency-ordering problem. A frontend that starts before its database is ready will crash. With separate docker run commands, ordering is manual: sleep, retry, or a custom script. Compose provides depends_on, which controls the order in which services start. With the condition: service_healthy option, Compose waits for a dependency’s healthcheck to pass before starting the dependent service. This is the difference between “the container is running” and “the service is ready” .

The lifecycle-management problem. Starting a stack is only part of the job. Stopping it, viewing logs across all services, executing a command in one service, and removing everything when done are all separate concerns. Compose provides a unified CLI for the entire lifecycle: up, down, logs, exec, ps, stop, restart. Each command operates on the project as a whole or on a specific service. The CLI is the control plane for the stack described in the file .

The trade-off. Compose is a single-host tool. It does not manage containers across multiple machines, and it does not provide the scheduling, scaling, and self-healing capabilities of Kubernetes or Swarm. It is also a development and small-scale production tool; for large-scale production, an orchestrator is the appropriate choice. The trade-off is between the simplicity of a single YAML file and the power of a distributed orchestration system. For the majority of development workflows, the simplicity wins.


a. The Compose application model

The compose.yaml file has a small number of top-level concepts: services, networks, volumes, configs, and secrets .

Services are the computing components of the application. A service is an abstract definition of a container: it specifies the image, the command, the ports, the environment variables, and the other runtime configuration. A service can be instantiated as one or more containers. In development, a service is usually one container; in production with an orchestrator, a service can have many replicas .

Networks are the communication channels between services. Compose creates a default network for the project, and every service is attached to it unless specified otherwise. Services on the same network can reach each other by service name. Custom networks can be defined for isolation โ€” a frontend network and a backend network, for example, where the database is only on the backend network .

Volumes are the persistent storage for services. A named volume is managed by Docker and survives container restarts and removal. A bind mount maps a host directory into the container. Volumes are declared at the top level and referenced in services. They can be shared between services when one container writes data that another reads .

Configs are configuration files mounted into containers. They are similar to volumes, but they are designed for non-sensitive configuration data that is managed by the Compose file itself.

Secrets are sensitive configuration data โ€” passwords, certificates, API keys โ€” mounted as files into containers. They are a distinct concept because the platform-specific handling of sensitive data differs from ordinary configuration .

The top-level structure of a minimal compose.yaml file:

name: myapp

services:
  web:
    image: nginx
    ports:
      - "8080:80"
    networks:
      - frontend

  api:
    build: ./api
    depends_on:
      - db
    networks:
      - frontend
      - backend

  db:
    image: postgres:16
    volumes:
      - db-data:/var/lib/postgresql/data
    networks:
      - backend

networks:
  frontend:
  backend:

volumes:
  db-data:

The name field sets the project name, which prefixes the names of all created resources. The services block defines the containers. The networks and volumes blocks define the top-level resources that services reference .

b. Services and the docker run mapping

Each service is the Compose equivalent of a docker run command. The mapping is direct: the image key maps to the image name, ports maps to -p, volumes maps to -v, environment maps to -e, and networks maps to --network .

services:
  database:
    image: mysql:8
    container_name: my-db
    ports:
      - "3306:3306"
    environment:
      MYSQL_ROOT_PASSWORD: secret
      MYSQL_DATABASE: appdb
    volumes:
      - db-data:/var/lib/mysql
    restart: unless-stopped

The service name database becomes the DNS hostname on the Compose network. A phpmyadmin service in the same project can connect to database:3306 without knowing the container’s IP address .

The build key is the alternative to image. It specifies a build context and optionally a Dockerfile path. Compose builds the image from the Dockerfile and uses it for the service .

services:
  api:
    build:
      context: ./api
      dockerfile: Dockerfile
    ports:
      - "3000:3000"

The depends_on key controls startup order. With the short form (depends_on: [db]), Compose starts the db container before the api container, but it does not wait for the database to be ready. With the long form and condition: service_healthy, Compose waits for the dependency’s healthcheck to pass .

services:
  api:
    depends_on:
      db:
        condition: service_healthy

  db:
    image: postgres:16
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U postgres"]
      interval: 5s
      timeout: 5s
      retries: 5
      start_period: 10s

c. The Compose CLI lifecycle

The Compose CLI is the control plane for the stack. The commands fall into a few categories: lifecycle, inspection, execution, and build .

Lifecycle commands start and stop the stack:

  • docker compose up creates and starts all services. The -d flag runs in detached mode. The --build flag rebuilds images before starting. The --force-recreate flag recreates containers even if the configuration has not changed .
  • docker compose down stops and removes containers and networks. The -v flag also removes named volumes. The --rmi all flag removes images built by Compose .
  • docker compose start, stop, and restart operate on existing containers without recreating them.

Inspection commands show the state of the stack:

  • docker compose ps lists the containers in the project.
  • docker compose logs shows the output from all services. The -f flag follows the output, and --tail=N limits the number of lines .
  • docker compose config validates the Compose file and shows the resolved configuration.

Execution commands run commands inside containers:

  • docker compose exec SERVICE COMMAND runs a command in a running container. docker compose exec db psql -U postgres opens a PostgreSQL shell .
  • docker compose run SERVICE COMMAND runs a one-off container with the same configuration. The --rm flag removes it after it exits.

Build commands manage images:

  • docker compose build builds or rebuilds the services that have a build key.
  • docker compose pull pulls the latest images for services that have an image key.

The project name is determined by the -p flag, the COMPOSE_PROJECT_NAME environment variable, the top-level name: field, or the directory name. It prefixes all created resources, so two Compose projects can run on the same host without interfering with each other .


Complete Example Session

# ============================================
# PART 1: A MINIMAL COMPOSE FILE
# ============================================

name: myapp

services:
  web:
    image: nginx:alpine
    ports:
      - "8080:80"

# The web service runs nginx and exposes port 80
# on the container to port 8080 on the host.


# ============================================
# PART 2: ADDING A DATABASE
# ============================================

services:
  web:
    image: nginx:alpine
    ports:
      - "8080:80"
    depends_on:
      - db

  db:
    image: postgres:16-alpine
    environment:
      POSTGRES_PASSWORD: secret
      POSTGRES_DB: appdb
    volumes:
      - db-data:/var/lib/postgresql/data

volumes:
  db-data:

# The web service depends on the db service.
# Compose starts db before web.
# The db service has persistent storage in a named volume.


# ============================================
# PART 3: BUILDING A CUSTOM SERVICE
# ============================================

services:
  api:
    build:
      context: ./api
      dockerfile: Dockerfile
    ports:
      - "3000:3000"
    environment:
      DATABASE_URL: postgres://postgres:secret@db:5432/appdb
    depends_on:
      - db

  db:
    image: postgres:16-alpine
    environment:
      POSTGRES_PASSWORD: secret
      POSTGRES_DB: appdb

# The api service is built from a Dockerfile in ./api.
# The DATABASE_URL uses the service name "db" as the hostname.
# Docker's embedded DNS resolves "db" to the container's IP.


# ============================================
# PART 4: HEALTHCHECK AND DEPENDENCY ORDERING
# ============================================

services:
  api:
    build: ./api
    depends_on:
      db:
        condition: service_healthy

  db:
    image: postgres:16-alpine
    environment:
      POSTGRES_PASSWORD: secret
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U postgres"]
      interval: 5s
      timeout: 5s
      retries: 5
      start_period: 10s

# The api service waits until the db healthcheck passes
# before starting. This prevents startup race conditions.


# ============================================
# PART 5: NETWORKS FOR ISOLATION
# ============================================

services:
  web:
    image: nginx
    networks:
      - frontend
    ports:
      - "8080:80"

  api:
    build: ./api
    networks:
      - frontend
      - backend

  db:
    image: postgres:16-alpine
    networks:
      - backend

networks:
  frontend:
  backend:

# The web service can reach the api service on the frontend network.
# The api service can reach the db service on the backend network.
# The web service cannot reach the db service directly.


# ============================================
# PART 6: THE FULL STACK
# ============================================

name: fullstack

services:
  web:
    image: nginx:alpine
    ports:
      - "80:80"
    volumes:
      - ./nginx.conf:/etc/nginx/nginx.conf:ro
    depends_on:
      - api
    networks:
      - frontend

  api:
    build:
      context: ./api
      dockerfile: Dockerfile
    environment:
      DATABASE_URL: postgres://app:secret@db:5432/appdb
      REDIS_URL: redis://cache:6379
    depends_on:
      db:
        condition: service_healthy
      cache:
        condition: service_started
    networks:
      - frontend
      - backend

  db:
    image: postgres:16-alpine
    environment:
      POSTGRES_USER: app
      POSTGRES_PASSWORD: secret
      POSTGRES_DB: appdb
    volumes:
      - db-data:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U app"]
      interval: 5s
      timeout: 5s
      retries: 5
      start_period: 10s
    networks:
      - backend

  cache:
    image: redis:7-alpine
    networks:
      - backend

volumes:
  db-data:

networks:
  frontend:
  backend:

# The web service serves static content and proxies to the api.
# The api service connects to the db and cache.
# The db service has a healthcheck and persistent storage.
# The cache service is a Redis instance on the backend network.


# ============================================
# PART 7: STARTING THE STACK
# ============================================

# $ docker compose up -d
# [+] Running 4/4
#  โœ” Container fullstack-db-1     Healthy
#  โœ” Container fullstack-cache-1  Started
#  โœ” Container fullstack-api-1    Started
#  โœ” Container fullstack-web-1    Started


# ============================================
# PART 8: VIEWING LOGS
# ============================================

# $ docker compose logs -f api
# api-1  | Server running on port 3000
# api-1  | Connected to database


# ============================================
# PART 9: EXECUTING A COMMAND
# ============================================

# $ docker compose exec db psql -U app -d appdb
# appdb=# \dt
#          List of relations
#  Schema |  Name   | Type  |  Owner
# --------+---------+-------+----------
#  public | users   | table | app
# (1 row)


# ============================================
# PART 10: STOPPING AND CLEANING UP
# ============================================

# $ docker compose down
# [+] Running 5/5
#  โœ” Container fullstack-web-1    Removed
#  โœ” Container fullstack-api-1    Removed
#  โœ” Container fullstack-db-1     Removed
#  โœ” Container fullstack-cache-1  Removed
#  โœ” Network fullstack_default    Removed

# $ docker compose down -v
# (also removes named volumes)

The ten parts show a minimal file, adding a database, building a custom service, healthcheck-based dependency ordering, network isolation, the full stack, starting it, viewing logs, executing a command, and cleaning up.


Quick Reference

Top-Level Compose Keys

KeyPurpose
nameProject name; prefixes created resources
servicesContainer definitions
networksNetwork definitions
volumesNamed volume definitions
configsConfiguration file definitions
secretsSensitive data definitions

Service Configuration Keys

KeyMaps to docker run
imageImage name
buildBuild from Dockerfile
ports-p
volumes-v
environment-e
networks--network
depends_on(no direct equivalent)
restart--restart
healthcheckHEALTHCHECK

Core CLI Commands

CommandPurpose
docker compose up -dCreate and start in detached mode
docker compose downStop and remove containers and networks
docker compose down -vAlso remove named volumes
docker compose psList containers
docker compose logs -fFollow logs
docker compose exec SERVICE CMDRun command in running container
docker compose buildBuild images
docker compose configValidate and view resolved file

Dependency Conditions

ConditionMeaning
service_startedContainer is running (default)
service_healthyHealthcheck passes
service_completed_successfullyExits with code 0

Best Practices

โœ… Do This:

# Use service names as hostnames
DATABASE_URL: postgres://user:pass@db:5432/appdb                          # โœ…
# Use healthcheck with depends_on for readiness
depends_on:
  db:
    condition: service_healthy                                            # โœ…
# Use named volumes for persistent data
volumes:
  - db-data:/var/lib/postgresql/data                                      # โœ…
# Set restart policies for production services
restart: unless-stopped                                                   # โœ…
# Use networks for isolation
networks:
  - frontend
  - backend                                                               # โœ…

โŒ Don’t Do This:

# Don't hardcode container IPs
DATABASE_URL: postgres://user:pass@172.18.0.2:5432/appdb                 # โŒ
# Don't rely on depends_on alone for readiness
depends_on:
  - db  # does not wait for db to be ready                               # โŒ
# Don't use bind mounts for database data
volumes:
  - ./data:/var/lib/postgresql/data  # permission issues                  # โŒ
# Don't store secrets in plain environment variables
POSTGRES_PASSWORD: mysecret  # use secrets in production                  # โŒ
# Don't use docker compose restart after changing compose.yaml
docker compose restart  # does not apply config changes                   # โŒ

Common Pitfalls

PitfallWhy It HappensFix
Service cannot reach another serviceDifferent networksAttach both to the same network
Container starts before dependency is readydepends_on without healthcheckUse condition: service_healthy
Data lost after downdown -v removes volumesOmit -v unless intentionally clearing data
Config changes not appliedrestart does not recreateUse up -d to recreate changed containers
Port conflictTwo services map the same host portChange the host port mapping
Project name collisionDefault is directory nameSet name: or use -p
Volume permission deniedBind mount overwrites container directoryUse named volumes for database data

Real-World Examples

1. Minimal Web App

services:
  web:
    image: nginx
    ports: ["8080:80"]

2. Web with Database

services:
  web:
    image: myapp
    depends_on: [db]
  db:
    image: postgres:16
    volumes: [db-data:/var/lib/postgresql/data]
volumes:
  db-data:

3. Build from Dockerfile

services:
  api:
    build: ./api

4. Service Discovery

environment:
  DATABASE_URL: postgres://user:pass@db:5432/appdb

5. Healthcheck Dependency

depends_on:
  db:
    condition: service_healthy

6. Network Isolation

networks:
  frontend:
  backend:

7. Named Volume

volumes:
  db-data:

8. Restart Policy

restart: unless-stopped

9. Detached Mode

docker compose up -d

10. Cleanup

docker compose down -v

Visual

The Compose Application Model

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  COMPOSE APPLICATION MODEL                                   โ”‚
โ”‚                                                              โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”    โ”‚
โ”‚  โ”‚  compose.yaml                                        โ”‚    โ”‚
โ”‚  โ”‚                                                       โ”‚    โ”‚
โ”‚  โ”‚  name: myapp                                         โ”‚    โ”‚
โ”‚  โ”‚                                                       โ”‚    โ”‚
โ”‚  โ”‚  services:                                           โ”‚    โ”‚
โ”‚  โ”‚    web:   image: nginx, ports: 8080:80               โ”‚    โ”‚
โ”‚  โ”‚    api:   build: ./api, depends_on: db               โ”‚    โ”‚
โ”‚  โ”‚    db:    image: postgres, volumes: db-data          โ”‚    โ”‚
โ”‚  โ”‚                                                       โ”‚    โ”‚
โ”‚  โ”‚  networks:                                           โ”‚    โ”‚
โ”‚  โ”‚    frontend:                                         โ”‚    โ”‚
โ”‚  โ”‚    backend:                                          โ”‚    โ”‚
โ”‚  โ”‚                                                       โ”‚    โ”‚
โ”‚  โ”‚  volumes:                                            โ”‚    โ”‚
โ”‚  โ”‚    db-data:                                          โ”‚    โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜    โ”‚
โ”‚         โ”‚                                                    โ”‚
โ”‚         โ–ผ                                                    โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”    โ”‚
โ”‚  โ”‚  docker compose up                                   โ”‚    โ”‚
โ”‚  โ”‚                                                       โ”‚    โ”‚
โ”‚  โ”‚  Creates:                                            โ”‚    โ”‚
โ”‚  โ”‚    - Network myapp_frontend                          โ”‚    โ”‚
โ”‚  โ”‚    - Network myapp_backend                           โ”‚    โ”‚
โ”‚  โ”‚    - Volume myapp_db-data                            โ”‚    โ”‚
โ”‚  โ”‚    - Container myapp-web-1                           โ”‚    โ”‚
โ”‚  โ”‚    - Container myapp-api-1                           โ”‚    โ”‚
โ”‚  โ”‚    - Container myapp-db-1                            โ”‚    โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜    โ”‚
โ”‚                                                              โ”‚
โ”‚  The file describes the desired state. Compose reconciles    โ”‚
โ”‚  the actual state with the file.                             โ”‚
โ”‚                                                              โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Service Discovery by Name

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  SERVICE DISCOVERY BY NAME                                   โ”‚
โ”‚                                                              โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”    โ”‚
โ”‚  โ”‚  Compose default network: myapp_default              โ”‚    โ”‚
โ”‚  โ”‚                                                       โ”‚    โ”‚
โ”‚  โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”      โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”                โ”‚    โ”‚
โ”‚  โ”‚  โ”‚  web        โ”‚      โ”‚  api        โ”‚                โ”‚    โ”‚
โ”‚  โ”‚  โ”‚  nginx      โ”‚โ”€โ”€โ”€โ”€โ”€โ–บโ”‚  node       โ”‚                โ”‚    โ”‚
โ”‚  โ”‚  โ”‚             โ”‚      โ”‚             โ”‚                โ”‚    โ”‚
โ”‚  โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜      โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”˜                โ”‚    โ”‚
โ”‚  โ”‚                              โ”‚                        โ”‚    โ”‚
โ”‚  โ”‚                              โ”‚  connects to           โ”‚    โ”‚
โ”‚  โ”‚                              โ”‚  "db:5432"             โ”‚    โ”‚
โ”‚  โ”‚                              โ–ผ                        โ”‚    โ”‚
โ”‚  โ”‚                       โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”                 โ”‚    โ”‚
โ”‚  โ”‚                       โ”‚  db         โ”‚                 โ”‚    โ”‚
โ”‚  โ”‚                       โ”‚  postgres   โ”‚                 โ”‚    โ”‚
โ”‚  โ”‚                       โ”‚             โ”‚                 โ”‚    โ”‚
โ”‚  โ”‚                       โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜                 โ”‚    โ”‚
โ”‚  โ”‚                                                       โ”‚    โ”‚
โ”‚  โ”‚  Docker DNS resolves "db" to the db container's IP.  โ”‚    โ”‚
โ”‚  โ”‚  The IP can change; the name does not.               โ”‚    โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜    โ”‚
โ”‚                                                              โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Dependency Ordering

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  DEPENDENCY ORDERING                                         โ”‚
โ”‚                                                              โ”‚
โ”‚  WITHOUT service_healthy:                                    โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”    โ”‚
โ”‚  โ”‚  db starts โ†’ api starts immediately                  โ”‚    โ”‚
โ”‚  โ”‚  api tries to connect โ†’ db not ready โ†’ api crashes   โ”‚    โ”‚
โ”‚  โ”‚  api restarts โ†’ db not ready โ†’ api crashes again     โ”‚    โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜    โ”‚
โ”‚                                                              โ”‚
โ”‚  WITH service_healthy:                                       โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”    โ”‚
โ”‚  โ”‚  db starts โ†’ healthcheck runs โ†’ passes               โ”‚    โ”‚
โ”‚  โ”‚  api starts โ†’ connects successfully                  โ”‚    โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜    โ”‚
โ”‚                                                              โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”    โ”‚
โ”‚  โ”‚  depends_on:                                         โ”‚    โ”‚
โ”‚  โ”‚    db:                                               โ”‚    โ”‚
โ”‚  โ”‚      condition: service_healthy                      โ”‚    โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜    โ”‚
โ”‚                                                              โ”‚
โ”‚  Compose waits for the healthcheck to pass before            โ”‚
โ”‚  starting the dependent service.                             โ”‚
โ”‚                                                              โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

The CLI Lifecycle

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  COMPOSE CLI LIFECYCLE                                       โ”‚
โ”‚                                                              โ”‚
โ”‚  docker compose up -d                                        โ”‚
โ”‚    โ”‚                                                         โ”‚
โ”‚    โ”œโ”€โ”€ Pull/build images                                     โ”‚
โ”‚    โ”œโ”€โ”€ Create networks                                       โ”‚
โ”‚    โ”œโ”€โ”€ Create volumes                                        โ”‚
โ”‚    โ”œโ”€โ”€ Start services in dependency order                    โ”‚
โ”‚    โ”‚                                                         โ”‚
โ”‚    โ–ผ                                                         โ”‚
โ”‚  docker compose ps                                           โ”‚
โ”‚  docker compose logs -f                                      โ”‚
โ”‚  docker compose exec SERVICE CMD                             โ”‚
โ”‚    โ”‚                                                         โ”‚
โ”‚    โ–ผ                                                         โ”‚
โ”‚  docker compose down                                         โ”‚
โ”‚    โ”œโ”€โ”€ Stop containers                                       โ”‚
โ”‚    โ”œโ”€โ”€ Remove containers                                     โ”‚
โ”‚    โ”œโ”€โ”€ Remove networks                                       โ”‚
โ”‚    โ”‚                                                         โ”‚
โ”‚    โ–ผ                                                         โ”‚
โ”‚  docker compose down -v                                      โ”‚
โ”‚    โ””โ”€โ”€ Also remove named volumes                             โ”‚
โ”‚                                                              โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Summary

ItemValue
Compose filecompose.yaml (preferred) or docker-compose.yml
Declarative modelFile describes desired state; Compose reconciles
ServicesContainer definitions
Default networkCreated automatically; services reachable by name
VolumesNamed volumes for persistent data
docker compose upCreate and start the stack
docker compose downStop and remove containers and networks
depends_onStartup order; not readiness
condition: service_healthyWait for healthcheck to pass
Project namePrefixes all created resources

Key takeaways:

  • Compose is declarative. The compose.yaml file describes the desired state of the application. Compose reconciles the actual state with the file. Running docker compose up again applies changes .
  • Services are the building blocks. Each service is a container definition: image, ports, volumes, environment, and networks. The service name becomes the DNS hostname on the Compose network .
  • The default network enables service discovery. Compose creates a network for the project and registers each service’s name as a DNS alias. Containers reach each other by service name, not IP address .
  • depends_on controls order, not readiness. The short form only waits for the container to start. The condition: service_healthy form waits for the healthcheck to pass, which is the correct pattern for services that need their dependencies to be ready .
  • Volumes provide persistence. Named volumes are managed by Docker and survive container restarts. They are declared at the top level and referenced in services .
  • The CLI manages the lifecycle. up, down, ps, logs, exec, build, and config are the core commands. The project name determines the prefix for all created resources .
  • Compose is a single-host tool. It is designed for development, testing, and small-scale production. For multi-host orchestration, use Docker Swarm or Kubernetes.
  • The file is the source of truth. It can be version-controlled, shared, and reproduced. The setup is not a sequence of commands; it is a specification.

Remember: Docker Compose is the bridge between running containers and managing an application. A single compose.yaml file describes every service, network, and volume, and a single docker compose up command creates the entire stack. The declarative model means the file is not a script but a specification: it describes what the application should look like, and Compose makes it so. The default network provides service discovery by name, so containers can reach each other without hardcoded IPs. The depends_on and service_healthy options prevent startup race conditions by waiting for dependencies to be ready, not just running. For development, testing, and small-scale production on a single host, Compose is the standard tool. It turns a collection of docker run commands into a version-controlled, reproducible, and shareable configuration.


Was this helpful?


Stop using slow, ad-bloated tool sites! ๐Ÿคฎ

๐Ÿ”Ž Search “KandZ Tools” on Google to use many professional utilities for free.

KandZ.me is the ultimate minimalist hub for:
โœ… Finance (Mortgage, Interest, Inflation)
โœ… Tech (Base64, JSON, Dev Suite, IP)
โœ… Health (BMI, BMR, TDEE)
โœ… Productivity (Timer, Workspace, QR)

โšก๏ธ Fast & Private
๐Ÿ”’ No data leaves your device
๐Ÿ’Ž 100% Free

๐Ÿ”— Use it now: https://tools.kandz.me
๐Ÿ”– Bookmark itโ€”youโ€™ll need it later!