Validate a docker-compose.yml File
Docker Compose files are notoriously sensitive to indentation because YAML uses whitespace to define structure. A single extra or missing space breaks the file silently or with a confusing error. This example shows a multi-service compose file with volumes, environment variables, and port mappings. Validate before running docker compose up to catch errors early.
Example
version: '3.9'
services:
web:
image: node:20-alpine
ports:
- "3000:3000"
environment:
NODE_ENV: production
DATABASE_URL: postgres://db:5432/myapp
depends_on:
- db
volumes:
- ./app:/usr/src/app
db:
image: postgres:16
environment:
POSTGRES_DB: myapp
POSTGRES_USER: user
POSTGRES_PASSWORD: secret
volumes:
- pgdata:/var/lib/postgresql/data
volumes:
pgdata:FAQ
- Why does Docker Compose fail with an indentation error?
- YAML uses indentation to define hierarchy. Mixing tabs and spaces, or inconsistent indentation under a key, causes the parser to misread the structure and report confusing errors.
- What does depends_on do in Docker Compose?
- depends_on controls startup order, ensuring the db service starts before the web service. It does not wait for the database to be ready — use a health check or startup script for that.
- How do I use environment variables in docker-compose.yml?
- Reference variables from a .env file in the same directory using ${VARIABLE_NAME} syntax, or use the env_file key to load a file of variables into a container.
Related Examples
Validate a Kubernetes Pod Manifest
Kubernetes manifests are complex YAML documents where a formatting error causes ...
Validate a GitHub Actions WorkflowGitHub Actions workflows are YAML files where formatting errors prevent pipeline...
Format a Helm Values FileHelm values files configure Kubernetes charts and are injected into templates as...