Note The file can be named compose.yaml (preferred), compose.yml, docker-compose.yaml, or docker-compose.yml. Compose V2 is the current standard — use 'docker compose' (no hyphen) instead of the legacy 'docker-compose' binary.
Note Named volumes declared in the top-level volumes: key persist across restarts and compose down. Bind mounts (host paths) are useful for development but should not be used for database storage in production.
compose volumespersist datanamed volume composemount in compose
Note Compose creates a default network automatically, so all services can already talk to each other. Define custom networks when you want to isolate groups — here, web cannot reach db directly.
compose networksisolate servicesnetwork segmentationservice communication
Note Always quote port mappings in YAML — values like 80:80 can be misinterpreted as a base-60 number by the YAML parser. Binding debug ports to 127.0.0.1 prevents accidental exposure on the network.
compose portsexpose port composeport mapping composepublish port
services:
api:
environment:
NODE_ENV: production
DB_HOST: db
env_file:
- .env.local
Note Compose automatically reads a .env file in the same directory for variable substitution in the compose file itself (e.g., image: myapp:${VERSION}). The env_file key loads variables into the container's environment.
Note Plain depends_on only waits for the container to start, NOT for the application inside to be ready. Use condition: service_healthy with a healthcheck to wait until the database actually accepts connections.
depends onservice dependencystartup orderwait for serviceservice healthy
Build Configuration
syntax
services:
api:
build:
context: <path>
dockerfile: <file>
args:
KEY: value
Note Setting both build and image means Compose builds and tags the result with that image name. The context path is relative to the compose file location.
Note Services with profiles are not started by default. They only launch when you explicitly activate the profile. Great for optional tools like database GUIs, debug utilities, or test runners.
docker compose up [options]
docker compose down [options]
example
docker compose up -d
docker compose up -d --builddocker compose down
docker compose down -v
output
✔ Container myproject-db-1 Started
✔ Container myproject-api-1 Started
Note up -d runs in the background. Add --build to rebuild images before starting. down stops and removes containers and default networks. down -v ALSO removes named volumes — this destroys your database data permanently.
Note sync copies changed files into the container without rebuilding. rebuild triggers a full image rebuild when the specified file changes (e.g., when you add a dependency). This replaces many custom file-watching setups.
Note Compose automatically merges compose.override.yaml on top of compose.yaml. Use -f flags for other environments: docker compose -f compose.yaml -f compose.prod.yaml up. This keeps your base config DRY.
compose overridemultiple compose filesdev vs prodcompose mergeenvironment config