Why Compose is more than a shortcut

A real app is several boxes that need each other. Compose describes them in one file, with their trailers, their gates and their start order. That plan stays where you can read it next month.

Part 6 of 9 · Containers4 min read
compose.yamlweb:  ports: 8080  depends_on: dbdb:  volume: dbdata  healthcheckthe loading plankept in Git, not typed oncewebdbdbfind each other by namestarted is not ready:web waits for a healthy db

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:

Shell
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. 1docker compose up -d reads compose.yaml and creates and starts everything in it. The option -d runs it in the background.
  2. 2docker compose ps lists 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.

services: db: image: postgres:17-alpine volumes: [dbdata:…] healthcheck: pg_isready web: image: nginx:alpine ports: ["127.0.0.1:8080:80"] depends_on: db (healthy)a boxits trailerhow it says readya second boxa gate onto the back yardwait for db
Each part of the file makes one thing in the yard: a box per service, a trailer per volume, a gate per published port, and an order to wait in.

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?

Shell
docker compose exec web getent hosts db  
172.21.0.2        db  db

Here is what each marked part does:

  1. 1docker compose exec web runs one command inside the running web container.
  2. 2getent hosts db asks the container to look up the name db and 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.)

network app_default db webasks for "db"db → 172.21.0.2this machine127.0.0.1:8080no gate
Inside the fence, web finds db by name on the yard road. Only web has a gate, and it opens onto the back yard. db has no gate.

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:

Shell
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 postgres
db  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. 1--format prints only the fields we choose, written as a template. Here we print the service name and its status.
  2. 2pg_isready -U postgres asks the database if it accepts connections now. The option -U sets the user name.
  3. 3sleep 4 waits 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.

startedreadytimedbstartinghealthywebdepends_onconnect…no answer:fails and exitswebservice_healthywaitsconnects

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.

docker runcompose.yamldocker run -p 8080:80 nginxgit log3e1a add healthchecka1f3 web on 127.0.0.107bd first planhappens oncethe reason is gonestays in Gitevery change on record
docker run leaves no record. compose.yaml stays in Git with its history.

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_on waits for start, not for ready. A healthcheck with condition: service_healthy waits until the service answers.
  • docker run is an event. compose.yaml is 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.

New posts by email

One email when a post ships, about twice a month. Nothing else, and one click to leave.

Latest: Finding where a request breaks · RSS