Docker-compose, conditional statements? e.g. add volume only if condition
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Docker Compose does not support native conditional statements (if/else) in docker-compose.yml files. However, there are several workarounds to achieve conditional behavior: environment variable substitution with default values, multiple compose files, profiles (Compose V2), and external templating tools. These techniques let you conditionally add volumes, ports, services, or other configuration based on the environment.
Method 1: Environment Variable Substitution
Docker Compose supports variable substitution with default values using ${VAR:-default} syntax:
The /dev/null trick works for "disabling" a volume, but it is a hack — the mount still exists, it just points to nothing.
Method 2: Multiple Compose Files
Docker Compose can merge multiple files. Use a base file and environment-specific overrides:
docker-compose.yml (base)
docker-compose.dev.yml (development overrides)
docker-compose.prod.yml (production overrides)
The second file merges into the first. Properties in the override file take precedence.
Method 3: Profiles (Compose V2)
Compose profiles let you conditionally enable services:
Services without a profiles key always start. Services with profiles only start when their profile is active.
Method 4: Using .env Files
Create different .env files for each environment:
.env.dev
.env.prod
Method 5: YAML Anchors and Extensions
Use YAML anchors to define reusable blocks:
Method 6: Docker Compose with Bash Script
Wrap docker compose in a script for complex conditional logic:
Method 7: Conditional Volume with Empty Variable
A cleaner way to conditionally add volumes using variable substitution:
Comparison of Approaches
| Method | Complexity | Flexibility | Best For |
| Env var substitution | Low | Limited | Simple value changes |
| Multiple compose files | Medium | High | Environment-specific configs |
| Profiles | Low | Medium | Optional services |
| .env files | Low | Medium | Per-environment defaults |
| Bash wrapper | High | Highest | Complex conditional logic |
Common Pitfalls
- Variable must be defined:
${VAR}without a default raises an error ifVARis unset. Always use ${VAR:-default}to provide a fallback. - Compose file merge order matters: The last file wins when properties conflict. Put overrides in the later file:
docker compose -f base.yml -f override.yml up. - Profiles and depends_on: If a profiled service depends on another profiled service, both profiles must be active. Otherwise, the dependency is not started and the service fails.
- Empty volumes list: You cannot use a variable to conditionally include or exclude an entire YAML key (like
volumes:). The key must exist — you can only control its values. - Docker Compose V1 vs V2: Profiles are only available in Docker Compose V2 (
docker compose, notdocker-compose). Thenameproperty and some variable substitution features are also V2-only.
Summary
- Docker Compose has no native if/else — use environment variables, multiple files, or profiles
- Use
${VAR:-default}for simple conditional values - Use multiple compose files (
-f base.yml -f override.yml) for environment-specific configuration - Use profiles to conditionally enable optional services like debugging tools or monitoring
- For complex logic, wrap
docker composein a shell script

