Docker Compose: multiple services with one command
How to describe a bundle of an application, database, and web server in one file, bring it up with one command, and update it without manual rebuilding.
Updated August 23, 2026
You can start a single service with a long docker run command. Two services that need to see each other — that no longer works: you'll forget the parameters the next day. Compose moves the entire description into a file you can read, edit, and keep in a repository.
Minimal example
The /opt/myapp directory, with compose.yaml in it:
services:
db:
image: postgres:17
restart: unless-stopped
environment:
POSTGRES_PASSWORD: ${DB_PASSWORD}
POSTGRES_DB: app
volumes:
- db-data:/var/lib/postgresql/data
healthcheck:
test: ["CMD-SHELL", "pg_isready -U postgres"]
interval: 10s
retries: 5
app:
image: ghcr.io/example/app:1.4.2
restart: unless-stopped
depends_on:
db:
condition: service_healthy
environment:
DATABASE_URL: postgres://postgres:${DB_PASSWORD}@db:5432/app
ports:
- "127.0.0.1:8080:8080"
volumes:
db-data:
Next to it, the .env file:
DB_PASSWORD=long-random-password
chmod 600 .env
docker compose up -d
docker compose ps
docker compose logs -f app
What matters here
- Names instead of addresses. The application reaches the database by the name
db— Compose creates a shared network where service names resolve automatically. You don't need to publish the database port externally, and you shouldn't. restart: unless-stopped. Without it, nothing will come back up after the machine reboots.depends_onwith a health check. A regulardepends_onwaits only for the container to start, not for the database to be ready to accept connections. The application starts earlier and crashes.- Exact image versions.
postgres:17, notpostgres:latest. Withlatest, the nextpullwill one day bring up a major version that won't open the old data. - Publishing on
127.0.0.1. Expose a port externally only through a web server with a certificate.
Everyday commands
docker compose up -d # bring up or apply changes
docker compose down # stop and remove containers (volumes intact)
docker compose down -v # ...and remove volumes too
docker compose restart app # restart one service
docker compose exec app sh # go inside
docker compose pull && docker compose up -d # update images
docker compose config # show final file with substitutions
docker compose config is the best way to check the file before starting: it will show what the variables expanded to and whether any indentation was lost.
docker compose down -vdeletes volumes along with the database. This is irreversible. Before any operation with-v, make sure you have a copy — see backups.
Environment files
It's convenient to separate the same stack for testing and production with overlays:
docker compose -f compose.yaml -f compose.prod.yaml up -d
The second file overrides only the differences: memory limits, published ports, and log level.
Common mistakes
- Secrets in
compose.yamlthat's stored in a repository. Keep them in.envwith permissions 600 and don't commit it. - Mixing
docker-composeanddocker compose. The old v1 version doesn't understand some syntax and stores projects under different names. Install the plugin — see Docker installation. - Data in the container instead of a volume. After
docker compose down, it will disappear. See volumes. - No memory limits. One service eats all the machine's memory, and the kernel kills its neighbors. See resource limits.