Sub topic 1
Why Compose: from docker run to a declarative stack
Prerequisites
This chapter assumes you can:
- Use a terminal.
- Explain what a container and an image are.
- Write and build a simple Dockerfile.
- Read YAML.
- Run
docker compose versionsuccessfully, which means Docker Engine or Docker Desktop with the Compose plugin is installed.
The problem with running containers one by one
A real application rarely fits in one container. A web service usually needs a database, and often a cache or a worker too. With plain docker run, you start each piece by hand and wire the pieces together yourself.
Here is a web service and a Postgres database started that way:
docker network create shop-net
docker volume create db-data
docker run -d --name db --network shop-net \
-e POSTGRES_USER=app -e POSTGRES_PASSWORD=change-me -e POSTGRES_DB=app \
-v db-data:/var/lib/postgresql/data \
postgres:16
docker build -t shop-web .
docker run -d --name web --network shop-net -p 8000:8000 \
-e DATABASE_URL=postgres://app:change-me@db:5432/app \
shop-webThis works, but it has weaknesses:
- Order matters. You must create the network and volume before the containers that use them.
- Knowledge lives in your shell history. A teammate has to copy these commands exactly, and any typo gives a different setup.
- Cleanup is manual. You stop and remove each container, then the network and the volume.
- It does not scale with the app. Each new service adds another long command.
The declarative alternative
Docker Compose is a tool that reads one YAML file describing your application's containers, networks, and volumes, and then creates them with one command. The file is declarative: it states the setup you want, and Compose works out the steps to reach it.
This is the same stack as a compose.yaml file:
name: shop
services:
web:
build: .
ports:
- "8000:8000"
environment:
DATABASE_URL: postgres://app:change-me@db:5432/app
db:
image: postgres:16
environment:
POSTGRES_USER: app
POSTGRES_PASSWORD: change-me
POSTGRES_DB: app
volumes:
- db-data:/var/lib/postgresql/data
volumes:
db-data:Start everything, then remove it:
docker compose up -d
docker compose downWhat Compose does for you
When you run docker compose up, Compose does the following:
- It reads the file and uses its
name(hereshop) as the project name. Resource names are prefixed with it, such asshop_defaultfor the network. - It creates a default network and connects every service to it.
- It creates the named volumes, builds images where
buildis given, and pulls the rest. - It starts the containers.
Because all services share that network, web reaches the database at the hostname db, the service name. You did not create or name anything by hand.
The file is plain text, so you can commit it to version control. A new teammate then needs only docker compose up.
Common mistakes
- Using
localhostfor another service. Inside a container,localhostis that container itself. Use the service name, such asdb. - Indentation errors. YAML uses spaces, never tabs. A misplaced key silently changes meaning or fails to parse.
- Expecting
downto delete data.docker compose downremoves containers and networks but keeps named volumes. Your database files survive.
Exercise
In the stack above, which hostname does the web service use to reach Postgres, and why does it work without any manual network setup?
Show answer
It uses db, the service name. Compose creates a default network for the project and attaches every service to it. Docker's built-in DNS on that network resolves each service name to its container.
Exercise
You run docker compose down, then docker compose up -d. Is the data in db-data still there?
Show answer
Yes. down removes containers and the network but keeps named volumes unless you add -v. The new db container mounts the same volume.