How to Layer Multiple Compose Files With -f in CI
Passing several -f files merges them in order, so a small CI override can tweak the base stack without duplicating it.
Keep the shared stack in docker-compose.yml and put CI-only changes in docker-compose.ci.yml, then pass both with -f so later files override earlier ones.
Steps
- Write CI-specific overrides in a second file.
- Pass
-f base -f overridein that order. - Later files win on scalar keys; lists and maps merge per Compose rules.
Override file
docker-compose.ci.yml
services:
api:
build:
target: test
environment:
LOG_LEVEL: debug
ports: []CI step
.github/workflows/ci.yml
steps:
- run: docker compose -f docker-compose.yml -f docker-compose.ci.yml up -d --waitGotchas
- Set
COMPOSE_FILE=docker-compose.yml:docker-compose.ci.ymlto avoid repeating-fon every command. - Compose merges
environmentmaps but replacesportslist entries, so know which keys merge vs override.
Related guides
How to Use Compose Profiles to Toggle Services in CIGroup optional services under Compose profiles so CI can start only what a given job needs with --profile, ke…
How to Pass Environment Variables and Secrets to Compose in CIFeed CI secrets into docker compose safely using the shell environment and env_file, and mount real secrets w…
How to Run Parallel Compose Stacks Per CI JobRun isolated Compose stacks in parallel CI jobs by giving each a unique project name with -p or COMPOSE_PROJE…