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.
A real-ish stack
Section titled “A real-ish stack”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: trueThings 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. dbis reachable as the hostnamedbfromapp. Compose networks come with DNS. See Networks.- The database publishes no ports. It doesn’t need to. Only
apptalks to it, over the internal network. Every published database port is a future incident. appbinds to127.0.0.1. nginx on the host proxies to it. The world doesn’t get to talk to it directly.internal: trueonbackendmeans containers on it can’t reach the internet. A database doesn’t need to phone home.
Commands
Section titled “Commands”docker compose up -d # create/update and start everything in the backgrounddocker compose psdocker compose logs -f appdocker compose exec db psql -U app # shell/command in a running servicedocker compose pull # fetch newer images (for the tags you pinned, right?)docker compose up -d # ...then recreate only what changeddocker compose restart app # restarts the container but does NOT pick up compose.yaml changesdocker compose down # stop and remove containers + networks. Volumes survive.docker compose config # show the fully-resolved file with variables substituteddocker 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.
Variables and .env
Section titled “Variables and .env”Two different mechanisms. Everyone confuses them. I did for a year.
.envnext tocompose.yamlis read automatically for${VAR}substitution in the YAML file itself. It doesn’t magically become the container’s environment.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.
DB_PASSWORD=change-me-seriously- Add
.envto.gitignore. Commit a.env.examplewith fake values instead. ${VAR:?must be set}makes Compose refuse to start ifVARis missing. Use it for secrets, and you’ll never boot with an empty password again.${VAR:-default}supplies a default.
depends_on doesn’t mean ready
Section titled “depends_on doesn’t mean ready”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.
Updating a stack
Section titled “Updating a stack”cd /srv/stacks/appdocker compose pulldocker compose up -ddocker image prune -f # clean the old images you just replacedChange a tag, up -d, done. Compose recreates only the services whose config or image changed.
Layout I’ve settled on
Section titled “Layout I’ve settled on”/srv/stacks/├── app/│ ├── compose.yaml│ ├── .env # not in git│ └── .env.example # in git├── monitoring/│ └── compose.yaml└── proxy/ └── compose.yamlOne 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.
Profiles
Section titled “Profiles”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.