Sub topic 16
Where Compose fits beyond local development
This chapter assumes you can read a compose.yaml, run docker compose up, and understand services, volumes, networks, health checks, secrets, and override files from the earlier chapters. It covers what Compose does well outside your laptop, where it stops, and how to prepare a file for a single-host deployment.
What Compose is built for
Compose runs a set of containers on one Docker Engine. It does not schedule containers across several machines, replace a failed host, or balance load between hosts. That design is a good match for three jobs:
- Local development and test environments.
- Continuous integration (CI) runs that need a database or queue next to the code under test.
- Small deployments on a single server, such as an internal tool or a side project.
Preparing a file for a single server
A file that works on your laptop usually needs changes before it is safe to run on a server. Keep the shared definition in compose.yaml and put the differences in a separate file.
# compose.prod.yaml
services:
web:
image: registry.example.com/shop/web:1.4.2
restart: unless-stopped
ports:
- "8000:8000"
db:
restart: unless-stopped
environment:
POSTGRES_PASSWORD_FILE: /run/secrets/db_password
secrets:
- db_password
secrets:
db_password:
file: ./secrets/db_password.txtRun it by naming both files. Later files override earlier ones.
docker compose -f compose.yaml -f compose.prod.yaml up -dCheck each of these before you deploy:
- Use a pinned image tag such as
1.4.2, built once and pushed to a registry, instead ofbuild: .on the server. Avoidlatest, because you cannot tell which version is running. - Set
restart: unless-stoppedso containers come back after a crash or a reboot. - Pass secrets as files, not as environment variables in the file or in
.env. - Leave out development bind mounts and Compose Watch. If you use an override file named
compose.override.yaml, remember that Compose merges it automatically, so do not copy it to the server. - Keep named volumes for data, and back them up.
docker compose down -vdeletes them. - Run
docker compose configto confirm the merged result is what you expect.
Where Compose runs out
The table lists needs that Compose does not meet by itself.
| Need | Compose on one host | Typical alternative |
|---|---|---|
| Survive loss of the whole server | No. One host is a single point of failure. | Kubernetes, or managed container services |
| Spread replicas across machines | No. `--scale` starts copies on one host only. | Kubernetes, Docker Swarm, Amazon ECS |
| Zero-downtime rolling updates | Limited. `up -d` recreates changed containers, with a short gap. | An orchestrator with rolling deployments |
| Automatic scaling on load | No. | Orchestrator autoscalers |
| Central secret management | File-based only. | A secrets manager from your platform |
Deciding
Stay with Compose when one host can carry the load and a short outage during an update is acceptable. Move to an orchestrator when you need several hosts, rolling updates without downtime, or autoscaling. Your Compose file is still useful in that move, because the image names, ports, environment variables, and volumes it lists describe what the new platform must provide.
Exercise
A teammate copies the whole project directory, including compose.override.yaml, to a production server and runs docker compose up -d. Name two problems this can cause and one command that would have shown the final configuration.
Show answer
Compose merges compose.override.yaml automatically, so development settings such as source bind mounts and DEBUG variables reach production. Also, if compose.yaml uses build: ., the server builds the image itself instead of running a tested, tagged image from a registry. docker compose config prints the merged configuration so you can check it before starting.
Exercise
Your web service runs three replicas on one server, and the server loses power. Does Compose keep the application available? Explain.
Show answer
No. All replicas run on the same host, so they all stop when it fails. Compose does not move containers to another machine. Staying available through a host failure needs several hosts and an orchestrator.