Your app is a web server and a database. You need two long docker run commands. They need a volume for the database, a password and a published port for the web server. The database must also start first. You type the commands once and the app works. A month later the web server’s port is open to the whole network, and nobody can say who published it or why.
The loading plan
When the yard handles several boxes at once, nobody gives orders one by one. Bub pins a loading plan at the dock. The plan lists the boxes, which trailer connects to which box and where the gate goes. It also says which box must be ready before the next one is placed. That plan is a compose file, and Compose is the yard hand who follows it.
Save this as compose.yaml in an empty folder named app:
services:
db:
image: postgres:17-alpine
environment:
POSTGRES_PASSWORD: example
volumes:
- dbdata:/var/lib/postgresql/data
healthcheck:
test: ["CMD", "pg_isready", "-U", "postgres"]
interval: 2s
web:
image: nginx:alpine
ports:
- "127.0.0.1:8080:80"
depends_on:
db:
condition: service_healthy
volumes:
dbdata:
Everything from part 5 is written down here: the trailer (dbdata) and the gate onto the back yard (127.0.0.1:8080). Each entry under services becomes a container, one per service here. Then start everything:
docker compose up -d
docker compose ps Network app_default Created Volume app_dbdata Created Container app-db-1 Created Container app-web-1 Created Container app-db-1 Started Container app-db-1 Waiting Container app-db-1 Healthy Container app-web-1 Started NAME IMAGE COMMAND SERVICE CREATED STATUS PORTS app-db-1 postgres:17-alpine "docker-entrypoint.s…" db 3 seconds ago Up 2 seconds (healthy) 5432/tcp app-web-1 nginx:alpine "/docker-entrypoint.…" web 3 seconds ago Up Less than a second 127.0.0.1:8080->80/tcp
Here is what each marked part does:
- 1
docker compose up -dreadscompose.yamland creates and starts everything in it. The option-druns it in the background. - 2
docker compose pslists the containers of this project and their state.
We removed the Creating and Starting lines from the output. Compose made a network, a volume and two containers, all named after the folder, app. It started db, waited until db was healthy, and only then started web.
The yard road
The first line of that output was a network, app_default. This network is a road inside the yard that only these boxes use. On that road, every box answers to its service name:
web was never told db's address. How does it find db?
docker compose exec web getent hosts db 172.21.0.2 db db
Here is what each marked part does:
- 1
docker compose exec webruns one command inside the runningwebcontainer. - 2
getent hosts dbasks the container to look up the namedband print the address it finds.
From inside web, the name db resolves to the database container’s address on the yard road. So the app’s connection string says db:5432, not an IP that changes on every restart. db has no gate. Nothing is published for it, so no port on your machine’s own addresses leads to it. Other machines on your network cannot reach it either. Only web has a gate, and it opens onto the back yard. (On a Linux host, the host itself can still reach the container’s address on the yard road directly. On Docker Desktop it cannot.)
Started is not ready
depends_on on its own waits only until the other container has started. A started Postgres is not yet a database you can use. The first time, it needs a few seconds to set up its files. Remove everything with its data, using docker compose down -v, so the database starts empty. Then start only db. Ask it at once, and again a moment later:
docker compose up -d db
docker compose ps db --format '{{.Service}} {{.Status}}'
docker compose exec db pg_isready -U postgres
sleep 4
docker compose ps db --format '{{.Service}} {{.Status}}'
docker compose exec db pg_isready -U postgresdb Up Less than a second (health: starting) /var/run/postgresql:5432 - no response db Up 4 seconds (healthy) /var/run/postgresql:5432 - accepting connections
Here is what each marked part does:
- 1
--formatprints only the fields we choose, written as a template. Here we print the service name and its status. - 2
pg_isready -U postgresasks the database if it accepts connections now. The option-Usets the user name. - 3
sleep 4waits four seconds before the next command.
The container was running before the database was ready. So the file has a healthcheck, pg_isready every two seconds, and web waits with condition: service_healthy. Without the healthcheck, an app that connects at start gets no answer, fails and exits.
db starts. The container is running, but Postgres is still preparing its files and gives no answer.
An event and a description
Here Compose stops being a shortcut. A docker run command leaves no record of why it was typed. docker inspect can show a container’s settings while the container exists. It never shows why the settings were chosen. After the container is removed, it shows nothing.
compose.yaml is a description. It stays in the folder, goes into Git next to the code, and appears in code review. When a gate opens onto the street, git log -p compose.yaml says who opened it and when.
Clean up with docker compose down -v in the app folder.
Key points
- A compose file is the loading plan: one entry per container, plus the volumes, published ports and start order they need.
- Compose puts the containers of one app on their own network, where each one is reachable by its service name.
depends_onwaits for start, not for ready. A healthcheck withcondition: service_healthywaits until the service answers.docker runis an event.compose.yamlis a description you keep in Git, so every open gate has an author and a date.
Next, in part 7: building your own box well, with a good Dockerfile.