Skip to content

Compose Stacks

Compose is a YAML file describing your containers, their networks, and their volumes. It’s the closest thing this industry has to writing down what you did, and it’s the reason I can go on vacation.

compose.yaml:

services:
app:
image: ghcr.io/example/app:2.4.1
restart: unless-stopped
env_file: .env
environment:
DATABASE_URL: postgres://app:${DB_PASSWORD}@db:5432/app
depends_on:
db:
condition: service_healthy
networks:
- frontend
- backend
ports:
- "127.0.0.1:8080:8000"
db:
image: postgres:17
restart: unless-stopped
environment:
POSTGRES_USER: app
POSTGRES_PASSWORD: ${DB_PASSWORD}
POSTGRES_DB: app
volumes:
- pgdata:/var/lib/postgresql/data
networks:
- backend
healthcheck:
test: ["CMD-SHELL", "pg_isready -U app -d app"]
interval: 10s
timeout: 5s
retries: 5
volumes:
pgdata:
networks:
frontend:
backend:
internal: true

Things to notice:

  • No version: key. It’s obsolete. Compose ignores it and prints a warning. Delete it from your old files and enjoy the silence.
  • db is reachable as the hostname db from app. Compose networks come with DNS. See Networks.
  • The database publishes no ports. It doesn’t need to. Only app talks to it, over the internal network. Every published database port is a future incident.
  • app binds to 127.0.0.1. nginx on the host proxies to it. The world doesn’t get to talk to it directly.
  • internal: true on backend means containers on it can’t reach the internet. A database doesn’t need to phone home.
Terminal window
docker compose up -d # create/update and start everything in the background
docker compose ps
docker compose logs -f app
docker compose exec db psql -U app # shell/command in a running service
docker compose pull # fetch newer images (for the tags you pinned, right?)
docker compose up -d # ...then recreate only what changed
docker compose restart app # restarts the container but does NOT pick up compose.yaml changes
docker compose down # stop and remove containers + networks. Volumes survive.
docker compose config # show the fully-resolved file with variables substituted

docker compose config is the most underrated command here. When something’s “not picking up” a variable, run it and look at what Compose actually sees.

Two different mechanisms. Everyone confuses them. I did for a year.

  1. .env next to compose.yaml is read automatically for ${VAR} substitution in the YAML file itself. It doesn’t magically become the container’s environment.
  2. env_file: in a service loads variables into the container’s environment.

In the example above, .env does both jobs: it fills in ${DB_PASSWORD} in the YAML, and env_file: .env passes everything into app.

.env
DB_PASSWORD=change-me-seriously
  • Add .env to .gitignore. Commit a .env.example with fake values instead.
  • ${VAR:?must be set} makes Compose refuse to start if VAR is missing. Use it for secrets, and you’ll never boot with an empty password again.
  • ${VAR:-default} supplies a default.

Plain depends_on: [db] only waits for the db container to start, not for Postgres to accept connections. Your app races ahead, fails to connect, and crashes. Use condition: service_healthy with a healthcheck, as above.

Also, make your app retry its connection anyway. Databases restart. Networks blip. Software that gives up on the first try is going to page somebody.

Terminal window
cd /srv/stacks/app
docker compose pull
docker compose up -d
docker image prune -f # clean the old images you just replaced

Change a tag, up -d, done. Compose recreates only the services whose config or image changed.

/srv/stacks/
├── app/
│ ├── compose.yaml
│ ├── .env # not in git
│ └── .env.example # in git
├── monitoring/
│ └── compose.yaml
└── proxy/
└── compose.yaml

One directory per stack. The directory name becomes the project name, which prefixes the container, network, and volume names. Rename the directory and Compose thinks it’s a brand new project with brand new, empty volumes. Ask me how I know. Actually, don’t. Set name: app at the top of compose.yaml to pin it.

For things you only want sometimes (debug tools, one-off jobs):

services:
adminer:
image: adminer
profiles: [debug]
networks: [backend]

docker compose --profile debug up -d includes it. A plain up -d ignores it.