Validate a docker-compose.yml File
Docker Compose is the standard tool for defining and running multi-container applications in development and testing environments, and its configuration files are among the most indentation-sensitive files a developer encounters. A single extra space, a tab character where spaces are expected, or a misaligned block can mean the difference between a healthy container stack and an error that takes twenty minutes to diagnose because YAML's whitespace-based structure produces error messages that rarely point directly at the problem. This example defines a two-service stack: a Node.js web application and a PostgreSQL database. The services key at the top level contains two child blocks (web and db), each indented by two spaces. Within the web service, nested keys like ports, environment, depends_on, and volumes are each indented by a further two spaces from their parent. Arrays like the port mapping list use dash-prefixed items indented from the key. Key components explained: ports: "3000:3000" maps host port 3000 to container port 3000 using the format "HOST:CONTAINER". environment sets environment variables inside the container — note that DATABASE_URL references the db service by name because Docker Compose creates a shared network where services can reach each other by service name. depends_on ensures the db container starts before web, though it doesn't wait for the database to be ready to accept connections — use a health check or startup script for that. volumes: ./app:/usr/src/app mounts a host directory into the container for live code reloading during development. The named volume pgdata at the bottom persists PostgreSQL data across container restarts. Without this, every docker compose down destroys all your database data. Real-world scenarios: a development environment that mirrors production by running the same database version; a CI/CD pipeline that uses Docker Compose to spin up integration test dependencies; a demo environment that non-developers can start with a single docker compose up command. Tips and pitfalls: never use tabs in YAML — configure your editor to insert spaces for .yml files. The YAML validator here catches mixed indentation and reports the line number so you can fix it immediately rather than spending time staring at the file trying to spot the invisible whitespace difference.
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
Kubernetes manifests are among the most complex YAML documents developers work w...
Validate a GitHub Actions WorkflowGitHub Actions workflows are defined in YAML files stored in .github/workflows/ ...
Format a Helm Values FileHelm is the package manager for Kubernetes — it packages Kubernetes manifests as...