Sub topic 1
A quick base Compose file
This chapter builds the base Compose file used in the rest of the tutorial. Later chapters add override files, profiles, and variables on top of it. Here you read it line by line, run it, and learn which parts are shared by every environment.
Prerequisites
This chapter assumes you already 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 basic shell commands.
You need Docker with the Compose plugin (the docker compose command, Compose v2). The example/web:1.0 image in this chapter is a placeholder. Substitute any web image you can pull to run it yourself.
What a base file is for
A base file holds the definitions that stay the same in development, test, and production. These are the service names, the images, the shared volumes, and the ports every environment needs. Anything that differs by environment goes in a separate file, which later chapters cover.
Keeping the base small is useful for two reasons:
- Differences between environments stay visible. You read one short override file instead of three copies of a full file.
- A change to a shared definition, such as an image tag, happens in one place.
The file
Compose looks for compose.yaml first in the project directory. It also accepts compose.yml, docker-compose.yaml, and docker-compose.yml for older projects. Use compose.yaml for new work. The version: key at the top is obsolete in current Compose and is ignored, so leave it out.
Here is the base file:
# 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:The file has two top-level keys: services and volumes. Compose also supports networks, configs, and secrets. This file needs none of them.
Reading it line by line
The web service
image: example/web:1.0pins an exact tag. A fixed tag means every environment starts from the same image. Avoidlatestin a base file, because it can change between machines.ports: - "8000:8000"publishes container port 8000 on host port 8000. The format isHOST:CONTAINER. Quote port mappings. YAML can read an unquoted22:22as a base-60 number, and quoting avoids that surprise.environmentsetsLOG_LEVEL: infoas a mapping. This is the shared default. Development and production will each change it later.depends_on: - dbmakes Compose startdbbeforeweb.
The db service
image: postgres:16uses the official PostgreSQL image on a fixed major version.POSTGRES_PASSWORDis read from your shell, as the next section explains.db-data:/var/lib/postgresql/datamounts a named volume at the directory where PostgreSQL stores its data.
The named volume
The empty db-data: entry under the top-level volumes key declares the volume. An entry with no value is valid and means "create it with default settings." The service-level line only refers to it by name. If you leave out the top-level declaration, Compose rejects the file because the service uses an undefined volume.
Docker prefixes the real volume name with the project name. The project name defaults to the directory name. In a directory called shop, the volume is shop_db-data.
Requiring a secret instead of hard-coding it
The line POSTGRES_PASSWORD: ${DB_PASSWORD:?set DB_PASSWORD} uses Compose variable interpolation. Compose replaces ${...} with a value from your shell environment or from a .env file before it starts anything.
The :? form makes the variable mandatory. If DB_PASSWORD is unset or empty, Compose stops with an error and starts nothing. The text after :? is the error message.
This matters for two reasons:
- A password never has to be committed to the repository.
- A missing variable fails immediately and loudly. Without
:?, Compose substitutes an empty string and PostgreSQL refuses to start, or starts with surprising settings.
Variable interpolation gets its own chapter. For now you only need to know that this value must exist before you run the project.
Running the base file
Set the variable for one command and start the project:
DB_PASSWORD=change-me docker compose up -dYou can also put the value in a file named .env next to compose.yaml. Compose reads it automatically:
# .env (do not commit this file)
DB_PASSWORD=change-meThen this command is enough:
docker compose up -d
docker compose ps
docker compose downIf you forget the variable, you see an error like this and nothing starts:
required variable DB_PASSWORD is missing a value: set DB_PASSWORDdocker compose down stops and removes the containers and the default network. It keeps the named volume db-data, so your database survives. Add -v to delete volumes too. That command destroys data, so use it deliberately.
What Compose creates for you
When you run docker compose up, Compose does more than start two containers. It also creates:
- A default network named
<project>_default. Both services join it. - DNS names for each service. The
webcontainer can reach the database at the hostnamedb, on PostgreSQL's port 5432. Noportsentry is needed for traffic between services. - The named volume, if it does not exist yet.
The ports entry on web is only for traffic from your host machine. The database has no ports entry, so nothing outside the Compose network can reach it. That is a sensible default for a base file. A development override can publish a database port later if you want to connect a local client.
Limits of depends_on
The short form of depends_on controls start order only. Compose starts the db container first, but it does not wait until PostgreSQL accepts connections. A web app that connects at startup can still fail on a slow first boot.
There are two usual fixes:
- Make the application retry its database connection.
- Add a
healthchecktodband use the longdepends_onform withcondition: service_healthy.
This base file keeps the short form to stay small, and the later chapters do not depend on the longer one. Knowing the limit now helps you avoid blaming override files for a startup race.
Checking that the file is valid
Before you add any layers, check that Compose parses the file the way you expect:
DB_PASSWORD=change-me docker compose configThis prints the file after interpolation, with defaults filled in and the short forms normalized. You will see DB_PASSWORD replaced by its value and the ports expanded into a longer form. Later chapters use docker compose config as the main way to inspect merged results, so get used to running it now.
What stays out of the base file
Look at what the base file leaves out:
- No
build:section. Production should run a prebuilt, tagged image. Developers who build locally can add that in an override. - No source-code bind mount. Mounting your working directory belongs in development only.
- No
restart:policy. A developer usually wants a crashed container to stay stopped so they can read the logs. Production usually wants it restarted. - No debug ports, and no database admin tools.
Each omission points at a difference between environments. The next chapter explains those differences and shows how to express them without copying the whole file.
Summary
The base file declares two services, one named volume, and one required variable. It pins image tags and keeps secrets out of the repository. It contains only what every environment shares. The rest of the tutorial adds layers on top of it without editing it.
Exercise
You run docker compose up -d in the project directory and Compose stops immediately with "required variable DBPASSWORD is missing a value: set DBPASSWORD". Name two ways to fix it, and say which part of the base file caused the error.
Show answer
The ${DB_PASSWORD:?set DB_PASSWORD} interpolation in the db service's POSTGRES_PASSWORD caused it. The :? form makes the variable mandatory. Fix it by exporting or prefixing the variable for the command (DB_PASSWORD=change-me docker compose up -d), or by adding DB_PASSWORD=change-me to a .env file in the project directory.
Exercise
After docker compose down, you run docker compose up -d again. Does the database keep its data? What command would delete it, and why is the volume entry listed twice in the file (once under db and once at the top level)?
Show answer
Yes. docker compose down removes containers and the default network but keeps named volumes, so db-data survives. docker compose down -v also removes volumes and deletes the data. The service-level entry (db-data:/var/lib/postgresql/data) mounts the volume into the container. The top-level db-data: entry declares that the volume exists. Both are required.
Exercise
A teammate wants the web app to connect to the database at startup and wonders if depends_on: - db guarantees PostgreSQL is ready. Is that correct?
Show answer
No. The short form only controls start order, so the db container starts first, but PostgreSQL may not yet accept connections. Either make the app retry, or add a healthcheck to db and use depends_on with condition: service_healthy.