What Is Docker Compose? Run and Manage Multi-Container Apps with compose.yaml
Learn how Compose defines services, networking, persistent volumes, startup checks, and a practical workflow for running and inspecting a multi-container app.
What Is Docker Compose? Run and Manage Multi-Container Apps with compose.yaml
Docker Compose is a tool for describing a multi-container application in compose.yaml and creating, running, and inspecting it as a group. Declare each service's image or build, ports, environment, networks, and persistent volumes; docker compose up then creates the required resources. A Dockerfile describes how to build one image, while a Compose file describes how application services work together.
What does a Compose file define?
A service is a role in the application, such as app or db, backed by an image and configuration. Compose may create one or more containers for a service, depending on its configuration and requested replicas.
Compose creates a default project network and connects the services. Containers on it can discover one another by service name, so an app can connect to db:5432 without a fixed database IP. Host-to-container traffic is different: a published port maps a host port to a port inside the container.
Data written only to a container's writable filesystem should not be treated as durable across replacement. Attach a named volume to data that must persist, such as a database directory. This separates the data lifecycle from the container lifecycle.
Example: web app and PostgreSQL
This configuration assumes the project directory contains the app's Dockerfile and code, and that the app reads DATABASE_URL and listens on port 3000. Change the example password for local development, never commit real credentials, and use an appropriate secret-management method in production.
compose.yaml:
services:
app:
build: .
ports:
- "${APP_PORT:-8080}:3000"
environment:
DATABASE_URL: postgres://app:${POSTGRES_PASSWORD}@db:5432/app
depends_on:
db:
condition: service_healthy
db:
image: postgres:18
environment:
POSTGRES_DB: app
POSTGRES_USER: app
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:?set POSTGRES_PASSWORD in .env}
volumes:
- db-data:/var/lib/postgresql/data
healthcheck:
test: ["CMD-SHELL", "pg_isready -U $${POSTGRES_USER} -d $${POSTGRES_DB}"]
interval: 5s
timeout: 3s
retries: 10
start_period: 10s
volumes:
db-data:Development-only .env example (do not commit):
APP_PORT=8080
POSTGRES_PASSWORD=replace-with-a-local-development-passwordNo database ports entry is defined, so in this example PostgreSQL is reachable only inside the Compose network. The app connects using service name db and container port 5432; open http://localhost:8080 from the host. The app image must already have a suitable start command and database driver/configuration.
Start and inspect the stack
From the project directory, check that Docker and the Compose plugin work, then validate the resolved configuration:
docker version
docker compose version
docker compose config -qdocker compose config -q parses the files and checks interpolation and consistency. Use docker compose config to inspect the expanded configuration. Since the result may contain substituted environment values, do not publish or log it if it includes secrets.
Build the app image and start services in the background:
docker compose up --build -d
docker compose ps
docker compose logs -f appup pulls or builds images as configured and creates or recreates the project's networks, volumes, and containers. Check service states and ports with ps, inspect logs, and verify the app's actual response. The database healthcheck and service_healthy condition help delay app startup until PostgreSQL is ready to accept connections. A healthcheck does not prove every query or business flow works; the application still needs to handle disconnects and retries.
Services on the same Compose network should use service names, not container IPs, which can change after recreation. If the app cannot reach PostgreSQL, inspect ps, logs db, and logs app; check that both services share a network and that the app uses host db. For diagnostics inside an already-running service, use docker compose exec app <command>.
Stop services without deleting data
Remove the project's containers and default network with:
docker compose downThe named db-data volume in this example remains, so a later up can reuse the database data. docker compose stop only stops existing containers; docker compose start starts those containers again.
docker compose down -v also removes declared named volumes and can delete database contents. Use it only when resetting the data is intended and backups or deletion are addressed. A named volume is not itself a backup; arrange separate database backup and restore procedures for data that must survive.
What depends_on does—and does not do
depends_on lets Compose order service creation and startup. Basic ordering does not mean a dependency is ready to accept work: a process can be running while its database is still initializing. Define a meaningful healthcheck and use condition: service_healthy when dependent startup should wait for readiness.
Startup checks cannot prevent later network loss, restarts, or expired connections. Applications still need retry/recovery behavior. If a service remains unhealthy, inspect its healthcheck command, whether that command exists in the image, and the service logs.
Development and production have different needs
Compose is useful for reproducible local development, testing, and CI, and Docker documents single-server production deployments too. Do not equate a single-host Compose deployment with a multi-node orchestrator that provides cluster scheduling and related policies. If you need multiple hosts, reassess deployment, failure recovery, and networking needs; consider a platform designed for that scope, such as Swarm or Kubernetes.
Production settings may differ: remove source-code bind mounts, adjust exposed ports and logging, add restart policy, or use an externally managed database. A compose.production.yaml can layer production changes over the base file. Review the merged result before running it:
docker compose -f compose.yaml -f compose.production.yaml config
docker compose -f compose.yaml -f compose.production.yaml up -dQuick decision table
| Need | Compose feature or command | Watch for |
|---|---|---|
| Run several containers as one app | services, docker compose up | Prepare each image and its runtime settings |
| Service-to-service connection | Service name on shared network | App uses db:5432; host uses a published port |
| Keep data when replacing containers | Named volume | down -v can delete that data |
| Check variable substitution and config | docker compose config | Rendered output may expose secrets |
| Wait for a database to be ready | healthcheck and service_healthy | Does not replace runtime retries |
| Inspect status and diagnose | ps, logs, exec | Check actual state and service names |
Summary
Compose groups services, networks, persistent data, and runtime settings in a declarative YAML file and manages their lifecycle with docker compose. Service-name discovery and named volumes make multi-container projects easier to reproduce and keep data separate from disposable containers. Combine config, ps, logs, and application-level checks; understand that down -v can erase volume data.
Sources
[Docker: How Compose works](https://docs.docker.com/compose/intro/compose-application-model/)
[Docker: Compose Quickstart](https://docs.docker.com/compose/gettingstarted/)
[Docker: Control startup and shutdown order](https://docs.docker.com/compose/how-tos/startup-order/)
[Docker: Networking in Compose](https://docs.docker.com/compose/how-tos/networking/)
[Docker: Set environment variables](https://docs.docker.com/compose/how-tos/environment-variables/set-environment-variables/)
[Docker: `docker compose config`](https://docs.docker.com/reference/cli/docker/compose/config/)
[Docker: `docker compose down`](https://docs.docker.com/reference/cli/docker/compose/down/)
[Docker: Use Compose in production](https://docs.docker.com/compose/how-tos/production/)
Scope: Docker Compose CLI and Specification documentation checked 2026-11-22. Features and YAML options can vary by installed Compose version. The services and password are illustrative; the app was not built or run here.