The docker-compose.yml file describes a multi-container application in one YAML file: the services (containers), the networks they share, the volumes that keep their data, and the configuration each one needs. With it, docker compose up starts the whole stack and docker compose down removes it, so every developer and CI job runs the same environment.
Today the file follows the Compose Specification, and the preferred file name is compose.yaml, although docker-compose.yml is still supported.
A Compose file has a small set of top-level elements:
| Element | Purpose |
|---|---|
services | The containers that make up the application |
networks | Custom networks for service-to-service traffic |
volumes | Named volumes that persist data |
configs and secrets | Configuration files and sensitive values mounted into services |
name | Optional project name |
The old version: '3' line is obsolete. Docker Compose v2 ignores it and prints a warning, so new files should leave it out.
This file runs a web application, a PostgreSQL database and persistent storage:
Start it with:
Add -v to docker compose down only when you also want to delete the named volumes and their data.
Each entry under services becomes one or more containers. The most used keys are:
image or build: pull an image, or build one from a Dockerfile.ports: publish container ports to the host in host:container form.environment and env_file: set environment variables.volumes: mount named volumes or host paths.depends_on: control start order, optionally waiting for a health check.restart: restart policy such as unless-stopped.command and entrypoint: override the image defaults.Plain depends_on only waits until the other container has started, not until the application inside is ready. Combine it with healthcheck and condition: service_healthy, as above, when the order matters.
Compose creates a default network for the project, and every service on it can reach the others by service name. In the example, web connects to db:5432. Define extra networks when you want to isolate groups of services, for example a frontend network for the web tier and a backend network for the database. A service on only the backend network cannot be reached from containers on the frontend network.
Container filesystems are discarded when a container is removed. A named volume such as db-data lives outside the container and survives docker compose down. Use it for databases and uploads. Use a bind mount, for example ./src:/app/src, when you want live code changes during development.
Compose reads a .env file in the project directory for variable substitution, such as ${TAG:-latest}. Do not commit secrets in it. Two features keep files manageable:
profiles: ["debug"] and start it only with docker compose --profile debug up.compose.override.yaml is merged automatically, and -f lets you combine several files, for example a base file and a production file.For local development, the develop.watch section can sync or rebuild files on change when you run docker compose watch.
docker-compose was the standalone Python tool (v1), which is end-of-life. docker compose is the Compose v2 plugin built into the Docker CLI, and you should use it.
No. It is obsolete in the Compose Specification, and Compose v2 ignores it.
It works for single-host deployments and small setups. For multi-node scaling and self-healing, teams usually move to Kubernetes or another orchestrator.
By service name through Docker's built-in DNS on the shared network, for example db or redis.
The Compose file is the single description of your multi-container application: services, networks, volumes and secrets. Drop the old version field, use health checks with depends_on, keep secrets out of the file, and run everything with docker compose up. It gives you repeatable environments from a laptop to CI.