Compose Basics
Multi-container apps with Docker Compose v2. Results appear in the same fence: same-line # comments when short, multiline # blocks below the sample when not. Fences are bash, dockerfile, or yaml as appropriate.
Search across all documentation pages
Multi-container apps with Docker Compose v2. Results appear in the same fence: same-line # comments when short, multiline # blocks below the sample when not. Fences are bash, dockerfile, or yaml as appropriate.
Define services under top-level services.
services:
api:
image: myapi:1.0
# one service named apiPublish host:container ports.
services:
web:
image: nginx:alpine
ports:
- "8080:80"
# host 8080 -> container 80Named or bind volumes.
services:
db:
image: postgres:16
volumes:
- pgdata:/var/lib/postgresql/data
volumes:
pgdata:
# persistent named volumeEnv vars inline or from file.
services:
api:
image: myapi:1.0
environment:
PORT: "3000"
env_file:
- .env
# PORT=3000 in containerStart order (not full readiness).
services:
api:
depends_on:
- db
db:
image: postgres:16
# db starts before apiBuild from Dockerfile in compose.
services:
api:
build:
context: .
dockerfile: Dockerfile
# docker compose build apiUser-defined bridge networks.
services:
api:
networks: [backend]
networks:
backend:
# services on backend can resolve each other by nameOptional service groups.
services:
debug:
profiles: ["debug"]
image: nicolaka/netshoot
# compose --profile debug upCompose healthcheck for dependencies.
services:
db:
image: postgres:16
healthcheck:
test: ["CMD-SHELL", "pg_isready -U postgres"]
interval: 5s
# Status: healthy when readyRestart policy.
services:
api:
restart: unless-stopped
# restarts on failure unless manually stoppedCreate and start.
# bash
docker compose up -d
# [+] Running N/NStop and remove resources.
# bash
docker compose down -v
# removes containers and volumes if -vService logs.
# bash
docker compose logs -f api
# follow api logsOne-off command on a service.
# bash
docker compose run --rm api npm test
# runs test container then removes itRender final config (validate).
# bash
docker compose config
# merged YAML to stdoutStack versions: Kubernetes 1.36.2 · Docker Engine 29.6.1 · Helm 3 · Compose v2 · containerd via CRI
Reviewed by Chris St. John·Last updated Jul 19, 2026