Why Compose exists

Intermediate · 10 min

Five docker run commands, typed in the right order with the right flags, every time. Compose turns that into one declared, versioned project.

Before thisUser-defined networks and container DNSNamed volumes

TechWorld with Nana · 11 min · from 1:12

Why Compose exists

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by TechWorld with NanaReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • Running a multi-container app by hand means remembering networks, volumes, names and flags for every container, in the right order, on every machine.
  • Compose replaces those commands with one declarative file, compose.yaml, that describes the desired state. `docker compose up` makes the engine match it.
  • Compose is a client. It talks to the same Docker Engine API as `docker run`, and its containers are ordinary containers you can inspect with the normal CLI.
  • Everything Compose creates belongs to a project. The project name defaults to the directory name and prefixes networks and volumes, such as shop_default and shop_pgdata.
  • Every project gets a default user-defined network, so services find each other by service name through Docker's embedded DNS.
  • `up` is idempotent. Running it again changes only what differs from the file, and `down` removes containers and networks but keeps named volumes unless you pass -v.
  • Compose v1 (`docker-compose`, Python) is gone. Current Compose is v2 or v5, run as `docker compose`, and ignores the old top-level `version:` key.

Five commands and a sticky note

An API, a Postgres database and a Redis cache. With the plain CLI, starting them takes something like this:

docker network create shop
docker volume create pgdata
docker run -d --name db --network shop \
  -e POSTGRES_PASSWORD=dev -v pgdata:/var/lib/postgresql postgres:18
docker run -d --name cache --network shop redis:8
docker run -d --name api --network shop -p 8000:8000 \
  -e DATABASE_URL=postgres://postgres:dev@db:5432/postgres \
  -e REDIS_URL=redis://cache:6379 registry.example.com/api:1.4.2

Every flag matters. Forget --network shop and the API cannot resolve db, because only user-defined networks have container DNS. Forget the volume and the database is gone on the next docker rm. Start the API before the network exists and it fails. Then a teammate needs the same setup, so the commands go into a README or a shell script. Now the setup lives in someone’s terminal history and drifts.

Updating is worse. To change the API’s tag you stop it, remove it, and retype the full docker run line, flags included. A shell script can start things, but it cannot tell you what is running versus what should be running.

Declare the end state instead

Compose turns the commands into a description of what should exist:

# compose.yaml
services:
  db:
    image: postgres:18
    environment:
      POSTGRES_PASSWORD: dev
    volumes:
      - pgdata:/var/lib/postgresql
  cache:
    image: redis:8
  api:
    image: registry.example.com/api:1.4.2
    ports:
      - "8000:8000"
    environment:
      DATABASE_URL: postgres://postgres:dev@db:5432/postgres
      REDIS_URL: redis://cache:6379

volumes:
  pgdata:

docker compose up -d reads the file and makes the engine match it. Think of it as the difference between giving someone driving directions and giving them the address. Directions break when a road is closed. An address lets the driver work out the route. Compose works out which networks, volumes and containers to create, and in what order.

Run up a second time and nothing happens, because everything already matches. Change the API’s tag and run up again, and Compose recreates only the API container. docker compose down removes the containers and the network. It keeps named volumes, so your data survives, unless you add -v.

The volume mount point is /var/lib/postgresql, not the /var/lib/postgresql/data older guides use. I pulled the postgres:18 image config from Docker Hub: it declares VOLUME /var/lib/postgresql and sets PGDATA=/var/lib/postgresql/18/docker, so the data lives under a versioned subdirectory from version 18 on.

What Compose adds, and what it doesn’t

Compose is a client. It does not run containers itself. It calls the same Engine API that docker run calls, so the containers are ordinary ones. docker ps, docker logs and docker exec all work on them. Compose finds its containers again through labels it sets, such as com.docker.compose.project and com.docker.compose.service.

What it adds is a project: a named group of resources. The name defaults to the directory holding the file, and it prefixes what Compose creates. I loaded a small project in a directory called shop with compose-go, the library the Compose CLI uses to read files, and printed the resolved model. The relevant lines:

name: shop
networks:
  default:
    name: shop_default
volumes:
  pgdata:
    name: shop_pgdata

Two consequences follow. Every project gets its own default network, so services reach each other by service name without you declaring a network. And two checkouts in directories with the same name collide: both would use shop_pgdata. Set a distinct name with docker compose -p or a top-level name: key when you need two copies side by side.

The same structure, the network, the volume, the names, would take a dozen flags with docker run. Here it is implied by the file.

v1, v2 and v5

Older tutorials type docker-compose with a hyphen and start their files with version: "3.8". That was Compose v1, written in Python and retired. The current CLI is a Docker plugin, run as docker compose. Docker’s history page explains that v2 (2020) ignores the version key and follows the Compose Specification, and that v5 (2025) is functionally identical to v2 but adds an official Go SDK. The number skipped to 5 to avoid confusion with the old file formats “2.x” and “3.x”. The release on GitHub at the time of writing is v5.8.1.

Compose is the tool for one host. It does not spread containers across machines or move them when a host dies. That limit, and how far you can push it, is the subject of Compose in production and its limits.

About the video

The embed is a clip, 1:12 to 11:58, of TechWorld with Nana’s “Ultimate Docker Compose Tutorial” from January 2024. Its chapters in that range are “What is Docker Compose”, “Demo - Without Docker Compose” and “Why Docker Compose”: she starts a two-container app with docker run commands, network and all, then makes the case for the file. That is this lesson’s argument, shown on screen. The rest of the course covers the file format, startup order and variables, which later lessons pick up. The video predates Compose v5 and the newer pre_start and jobs features, but nothing in the clip depends on them. English was judged from the channel and its English caption track.

What was run: the postgres:18 image config was pulled from Docker Hub’s registry API, and the project was loaded with a small Go program built on compose-go v2.17.0, the version Compose v5.8.1 uses. No Docker daemon was available, so no containers were started. Everything about up, down and labels is from the Compose documentation, checked 2026-11-28.

Where this fits

You know what Compose is for. Next, the file itself, section by section: the Compose file: services, networks and volumes.

Check yourself

  1. 01You run the same compose.yaml from two directories, ~/a/shop and ~/b/shop. Do the two projects share a database volume?

    Yes, by default. Both projects are named shop after their directory, so both use the volume shop_pgdata. Give one a different name with -p or the top-level name: key to separate them.

  2. 02A teammate says Compose containers are special and can't be debugged with docker exec. Are they right?

    No. Compose creates ordinary containers through the Engine API and labels them with com.docker.compose.project and com.docker.compose.service. docker ps, exec, logs and inspect all work on them.

  3. 03After editing one service's image tag, you run docker compose up -d again. What happens to the other services?

    Nothing. Compose compares each service's current containers with the file and recreates only the ones whose configuration changed.

  4. 04Why does an api container reach Postgres at db:5432 in Compose but not when both were started with plain docker run?

    Plain docker run puts containers on the default bridge, which has no name resolution between containers. Compose puts the project on its own user-defined network, where the embedded DNS resolves service names.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark