Compose Intro
Most real applications are not one container. A small online shop already needs a web front end, an API, a
database, a cache and maybe a background worker. You could start each one with a long docker run command,
create a network by hand, remember which container must start first and type the same flags again tomorrow. Or you
can write the whole application down once, in a single YAML file, and let Docker Compose do the bookkeeping.
Compose is a tool for defining and running multi-container applications. You describe the desired state (which services exist, which image each one runs, which ports, volumes, networks, variables and dependencies they have), and Compose turns that description into containers, networks and volumes on one Docker host. Edit the file, run the same command again, and Compose changes only what is different.
compose.yaml one Docker host
┌───────────────────────────┐ ┌──────────────────────────────────────┐
│ services: │ │ network shop_default │
│ web: image nginx │ up │ ┌────────────┐ ┌──────────────┐ │
│ api: build ./api │ ─────▶ │ │ shop-web-1 │──▶│ shop-api-1 │ │
│ db: image postgres │ │ └────────────┘ └──────┬───────┘ │
│ volumes: │ │ ┌───────▼──────┐ │
│ pgdata: │ │ │ shop-db-1 │ │
└───────────────────────────┘ │ └──────────────┘ │
│ volume shop_pgdata │
└──────────────────────────────────────┘This course and the Docker course
The Docker course has a short Compose chapter: Compose Basics, depends_on & Healthchecks, Env Files & Variables and Profiles. If you have never used Compose, read those first: they cover the everyday commands in a few pages. This course goes much deeper. It treats the Compose file as a precise language and looks at every important part of it:
| Section | What you learn |
|---|---|
| Compose Basics | installing the plugin, a first app, the file's structure, the CLI |
| Services | image vs build, ports, environment, interpolation, command/entrypoint, healthchecks, startup order, restart policies |
| Storage & Networking | volumes, bind mounts, configs, secrets, networks, service discovery |
| Multiple Environments | override files, extends, include, profiles, project names, YAML anchors |
| Development Workflow | develop.watch, debugging, logs, scaling |
| Production & Beyond | running Compose on a server, CI, security, and moving on to Swarm or Kubernetes |
You should be comfortable with images, containers, ports and volumes from the Docker course; this course does not re-explain what a container is.
How the examples work
Compose files in this course are real. Each compose example below is a compose.yaml (sometimes with extra
files such as .env), and its output was produced by the real docker compose config command. config is
Compose's "show me what you understood" command: it reads the file, fills in variables, applies defaults, converts
short forms into their long, canonical form and validates the result, without pulling an image or starting a
container. That makes it the best tool for learning Compose, and a great habit for real work too.
A few things to know about the examples:
- The files live in a folder called
demo, so the project name isdemounless the file says otherwise. - The environment is empty: no variables from your shell leak in. Variables come only from the example's files.
- An example may start with lines such as
# $ docker compose config --services; those are the commands that were run. Without them, plaindocker compose configwas used. - Commands that change things (
up,down,logs,exec) appear as sample output in ordinary code blocks. Container IDs, times and sizes in those blocks will differ on your machine.
Here is the smallest useful Compose file. Look at what Compose adds to it:
services:
web:
image: nginx:1.29
ports:
- "8080:80"name: demo
services:
web:
image: nginx:1.29
networks:
default: null
ports:
- mode: ingress
target: 80
published: "8080"
protocol: tcp
networks:
default:
name: demo_default
Three lines of service became a full description. The port mapping turned into a structured entry with a
protocol and a mode, a project name appeared, and so did a network called demo_default that you never declared.
Every Compose project gets a default network, and every service joins it, so services can reach each other by
name. You will meet all of these fields again in later lessons.
When Compose is the right tool
Compose manages containers on one Docker host. That covers a lot of ground:
- Local development: start the app and all its backing services with one command; every developer gets the same versions.
- Automated tests and CI: spin up a database and a message broker for integration tests, then throw them away.
- Small production deployments: a single VM running a handful of services with
restart: unless-stopped. - Demos and documentation: "clone the repo, run
docker compose up" is the friendliest README there is.
When you need several machines, automatic rescheduling after a node failure or rolling updates across a cluster, you have outgrown plain Compose. Docker Swarm reads (almost) the same file format as a stack, and Kubernetes uses its own manifests. The last section of this course, From Compose to Swarm and From Compose to Kubernetes, shows the path.
A model of what Compose does
At its heart Compose is a reconciliation loop: compare what the file asks for with what is running and act on the difference. The model below plays that loop for a tiny project. It only looks at the image of each service, but the real tool compares a hash of the entire service configuration.
def plan(desired, running):
actions = []
for name, image in desired.items():
if name not in running:
actions.append(f"create {name} ({image})")
elif running[name] != image:
actions.append(f"recreate {name} ({running[name]} -> {image})")
else:
actions.append(f"keep {name}")
for name in running:
if name not in desired:
actions.append(f"orphan {name} (removed with --remove-orphans)")
return actions
desired = {"web": "nginx:1.29", "api": "shop/api:1.5", "db": "postgres:18"}
running = {"web": "nginx:1.29", "api": "shop/api:1.4", "worker": "shop/worker:1.4"}
for step in plan(desired, running):
print(step)keep web
recreate api (shop/api:1.4 -> shop/api:1.5)
create db (postgres:18)
orphan worker (removed with --remove-orphans)
The real docker compose up -d prints similar decisions as Running, Recreated and Created lines, and it
warns about orphan containers that belong to the project but no longer appear in the file.
Compose versions in one paragraph
The original Compose was a Python program called docker-compose (with a hyphen). It was replaced by Compose
v2, a Go plugin for the Docker CLI that you call as docker compose (with a space); newer releases continue that
line. The file format is the open Compose Specification, which replaced the numbered version: "2" / "3.x"
formats. A top-level version: key is now obsolete: Compose ignores it and prints a warning, so leave it out.
Learn more: Installing Compose · Your First Compose App · The Compose File
Test yourself
What does docker compose config do?
- Starts the project in the background
- Reads, interpolates, normalises and validates the Compose file and prints the result, without creating anything
- Edits the Compose file in place
- Pulls every image in the file
Which network does a service join if the file declares no networks at all?
- The host network
- No network
- The project's default network, e.g. demo_default
- The bridge network docker0 only
When have you outgrown plain Compose?
- When you have more than three services
- When you need to run containers across several hosts with rescheduling and rolling updates
- When you use a database
- When you build your own images