Docker
Docker-compose
Conditional Statements
Configuration Management
DevOps

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}:

yaml
1services:
2  app:
3    image: myapp:latest
4    volumes:
5      - ${DATA_VOLUME:-/dev/null:/dev/null}
6    ports:
7      - "${APP_PORT:-8080}:8080"
8    environment:
9      - "DEBUG=${DEBUG_MODE:-false}"
bash
1# With volume mounted
2DATA_VOLUME=./data:/app/data docker compose up
3
4# Without volume (mounts /dev/null onto /dev/null, a no-op)
5docker compose up

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)

yaml
1services:
2  app:
3    image: myapp:latest
4    ports:
5      - "8080:8080"
6    environment:
7      - NODE_ENV=production

docker-compose.dev.yml (development overrides)

yaml
1services:
2  app:
3    volumes:
4      - ./src:/app/src        # Hot reload source code
5      - ./logs:/app/logs       # Local log directory
6    environment:
7      - NODE_ENV=development
8      - DEBUG=true
9    ports:
10      - "9229:9229"           # Debugger port

docker-compose.prod.yml (production overrides)

yaml
1services:
2  app:
3    restart: always
4    deploy:
5      replicas: 3
6      resources:
7        limits:
8          cpus: '2.0'
9          memory: 1G
bash
1# Development
2docker compose -f docker-compose.yml -f docker-compose.dev.yml up
3
4# Production
5docker compose -f docker-compose.yml -f docker-compose.prod.yml up

The second file merges into the first, and properties in the override file take precedence.

Method 3: Profiles

Profiles let you enable services conditionally:

yaml
1services:
2  app:
3    image: myapp:latest
4    ports:
5      - "8080:8080"
6
7  debug-tools:
8    image: nicolaka/netshoot
9    profiles:
10      - debug
11    network_mode: "service:app"
12
13  monitoring:
14    image: prom/prometheus
15    profiles:
16      - monitoring
17    ports:
18      - "9090:9090"
19
20  db-admin:
21    image: adminer
22    profiles:
23      - debug
24      - monitoring
25    ports:
26      - "8081:8080"
bash
1# Only the app service starts (no profile specified)
2docker compose up
3
4# Start app + debug tools + db-admin
5docker compose --profile debug up
6
7# Start app + monitoring + db-admin
8docker compose --profile monitoring up
9
10# Start everything
11docker compose --profile debug --profile monitoring up

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:

yaml
1services:
2  app:
3    image: myapp:latest
4    ports:
5      - "${APP_PORT:-8080}:8080"
6    environment:
7      - "LOG_LEVEL=${LOG_LEVEL:-info}"
8    volumes:
9      - ${MOUNT_SOURCE:-/dev/null:/dev/null}

.env.dev

env
1COMPOSE_FILE=docker-compose.yml:docker-compose.dev.yml
2APP_PORT=3000
3LOG_LEVEL=debug
4MOUNT_SOURCE=./src:/app/src

.env.prod

env
COMPOSE_FILE=docker-compose.yml:docker-compose.prod.yml
APP_PORT=80
LOG_LEVEL=warn

.env.prod leaves MOUNT_SOURCE unset, so production gets the /dev/null:/dev/null no-op instead of the source mount.

bash
1# Use dev config
2docker compose --env-file .env.dev up
3
4# Use prod config
5docker compose --env-file .env.prod up

Method 5: YAML anchors and extensions

Use YAML anchors to define reusable blocks:

yaml
1x-common-env: &common-env
2  LOG_LEVEL: info
3  TZ: UTC
4
5x-dev-volumes: &dev-volumes
6  - ./src:/app/src
7  - ./config:/app/config
8
9services:
10  app:
11    image: myapp:latest
12    environment:
13      <<: *common-env
14      NODE_ENV: ${NODE_ENV:-production}
15    volumes: *dev-volumes

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:

bash
1#!/bin/bash
2# deploy.sh
3
4ENV=${1:-dev}
5COMPOSE_FILES="-f docker-compose.yml"
6
7if [ "$ENV" = "dev" ]; then
8    COMPOSE_FILES="$COMPOSE_FILES -f docker-compose.dev.yml"
9    export MOUNT_VOLUMES=true
10fi
11
12if [ "$ENV" = "prod" ]; then
13    COMPOSE_FILES="$COMPOSE_FILES -f docker-compose.prod.yml"
14    export MOUNT_VOLUMES=false
15fi
16
17if [ "$ENABLE_MONITORING" = "true" ]; then
18    COMPOSE_FILES="$COMPOSE_FILES -f docker-compose.monitoring.yml"
19fi
20
21docker compose $COMPOSE_FILES up -d
bash
./deploy.sh dev
./deploy.sh prod
ENABLE_MONITORING=true ./deploy.sh prod

Method 7: Conditional volume with an empty variable

A cleaner way to add a volume conditionally with variable substitution:

yaml
1services:
2  app:
3    image: myapp:latest
4    volumes:
5      - app-data:/app/data
6      - ${EXTRA_VOLUME:-/dev/null:/dev/null}
bash
1# With extra volume
2EXTRA_VOLUME=./uploads:/app/uploads docker compose up
3
4# Without extra volume (maps /dev/null to /dev/null, essentially a no-op)
5docker compose up

Comparing the approaches

MethodComplexityFlexibilityBest For
Env var substitutionLowLimitedSimple value changes
Multiple compose filesMediumHighEnvironment-specific configs
ProfilesLowMediumOptional services
.env filesLowMediumPer-environment defaults
Bash wrapperHighHighestComplex conditional logic

Gotchas

  • Unset variables become empty strings: ${VAR} without a default is replaced with an empty string (with a warning) if VAR is 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 !reset tag clears a value from earlier files (volumes: !reset []), and !override replaces 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-level name property and some variable substitution features are V2-only.

Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design