Sub topic 1
A quick base Compose file
Prerequisites
This chapter assumes you know the following:
- Basic Docker concepts: images, containers, and volumes.
- Simple YAML: mappings, lists, and indentation.
- How to run
docker compose upanddocker compose downfor a multi-service app. - Environment variables and shell commands.
Why start with a base file
The rest of this tutorial layers extra files on top of one shared file. That shared file is the base. It should hold only what every environment needs: which services exist, which images they use, and how they connect. Development, test, and production files then change or add to it.
The diagram shows the idea.
The base file
This file defines a web service and a PostgreSQL database.
# compose.yaml (base)
services:
web:
image: example/web:1.0
ports:
- "8000:8000"
environment:
LOG_LEVEL: info
depends_on:
- db
db:
image: postgres:16
environment:
POSTGRES_PASSWORD: ${DB_PASSWORD:?set DB_PASSWORD}
volumes:
- db-data:/var/lib/postgresql/data
volumes:
db-data:Reading it piece by piece
servicesis the top-level mapping. Each key under it (web,db) is a service name. Compose uses these names for DNS between containers, sowebcan reach the database at the host namedb.imagenames what to run. The base file points at a published image and has nobuildkey. A development file can addbuildlater.portsmaps a host port to a container port. Quote the values. Unquoted numbers such as22:22can be read by YAML as a base-60 number.environmentsets variables inside the container.LOG_LEVEL: infois a safe default that other files can change.depends_oncontrols start order only. Compose startsdbbeforeweb, but it does not wait until PostgreSQL accepts connections.volumesat the service level mounts the named volumedb-datainto the data directory. The top-levelvolumes:key declares it, so the data survivesdocker compose down.
Requiring a secret without hard-coding it
The line POSTGRES_PASSWORD: ${DB_PASSWORD:?set DB_PASSWORD} is a variable reference with an error rule. Compose reads DB_PASSWORD from your shell or from a .env file. If the variable is unset or empty, Compose stops with the message after :?. No secret lives in the file, and nobody can start the stack by accident with a blank password.
Try it:
# Fails with: required variable DB_PASSWORD is missing a value: set DB_PASSWORD
docker compose config
# Works once the variable exists
DB_PASSWORD=devpass docker compose configdocker compose config prints the final configuration without starting anything. A later chapter covers it in detail.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| `required variable DB_PASSWORD is missing a value` | The variable is not set in the shell or `.env` | Export it or add it to `.env` |
| `web` crashes at start with a connection error | `depends_on` waited for the container, not for PostgreSQL | Retry in the app, or add a healthcheck |
| Port already allocated | Another process uses host port 8000 | Change the host side of the mapping |
| Data gone after `down -v` | `-v` deletes named volumes | Leave `-v` off unless you want a reset |
What the base file leaves out
The base file has no source-code mounts, no debug ports, and no restart policies. Those differ between environments, so they go in other files. The next chapters explain why environments differ and how to layer those files on top of this one.
Exercise
In the base file, what happens if you run docker compose up without setting DB_PASSWORD, and why is this better than writing a default password in the file?
Show answer
Compose stops before starting anything and prints "set DB_PASSWORD", because the :? form makes the variable required. This keeps the secret out of version control and prevents a stack from running with an empty or guessable password.