Docker Compose conditional statements: add a volume only if a condition is met
Codemia Knowledge Hub ·
Docker Compose has no native conditional statements (if/else) in docker-compose.yml. You get conditional behavior through workarounds instead: variable substitution with defaults, multiple compose files, profiles, .env files, YAML anchors or a wrapper script. These let you add volumes, ports, services or other configuration depending on the environment.
Method 1: Environment variable substitution
Compose supports variable substitution with defaults using ${VAR:-default}:
Put the whole source:target pair in the variable. Defaulting only the source, as in ${VOLUME_PATH:-/dev/null}:/app/data, does not disable anything: if the image already has an /app/data directory the container fails to start with Are you trying to mount a directory onto a file (or vice-versa)?, and otherwise /app/data becomes a device file that the app cannot write into. Mounting /dev/null onto /dev/null is still a hack (a mount exists), but it is harmless.
Method 2: Multiple compose files
Compose can merge several files. Use a base file plus 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, and properties in the override file take precedence.
Method 3: Profiles
Profiles let you enable services conditionally:
Services without a profiles key always start. Services with profiles start only when one of their profiles is active.
Method 4: Per-environment .env files
Create a separate .env file for each environment, and reference its variables from the base docker-compose.yml:
.env.dev
.env.prod
.env.prod leaves MOUNT_SOURCE unset, so production gets the /dev/null:/dev/null no-op instead of the source mount.
Method 5: YAML anchors and extensions
Use YAML anchors to define reusable blocks:
Anchors remove repetition but are not conditional on their own: a variable such as ${DEV_VOLUMES:-[]} expands to a string, and Compose rejects it with volumes must be a list. Put the anchored block in an override file or a profiled service when it should apply only sometimes.
Method 6: A Bash wrapper script
For complex conditional logic, wrap docker compose in a script:
Method 7: Conditional volume with an empty variable
A cleaner way to add a volume conditionally with variable substitution:
Comparing the 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 |
Gotchas
- Unset variables become empty strings:
${VAR}without a default is replaced with an empty string (with a warning) ifVARis unset, which can produce invalid values. Use${VAR:-default}to provide a fallback. - Merge order matters: the last file wins when properties conflict, so 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.
- A variable cannot drop a whole key: a variable cannot include or exclude an entire YAML key (like
volumes:). An override file can, though: since Compose 2.24 the!resettag clears a value from earlier files (volumes: !reset []), and!overridereplaces it instead of merging. - Older Compose versions: profiles need Compose 1.28.0 or later, and V2 (
docker compose) is the maintained version. The top-levelnameproperty and some variable substitution features are V2-only.
Related reading
- docker-compose container name use dash - instead of underscore _
- Docker-Compose Entrypoint/Command
- docker-compose for Detached mode
- docker-compose how to use minio in- and outside of the docker network
- Docker-Compose Postgresql import dump
- docker-compose, run a script after container has started?
- docker-compose INTERNAL ERROR cannot create temporary directory
- docker-compose kafka wait for zookeeper and schema-registry wait for kafka
System design interview problems on this topic
- Design a Container Orchestration System: solution and walkthrough
- Design a Code Deployment System: solution and walkthrough
- Design GitHub Actions: solution and walkthrough
- Design a Serverless Architecture Framework: solution and walkthrough

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseSystem Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.