Bird
Raised Fist0
Dockerdevops~10 mins

Multiple Compose files (override) in Docker - Step-by-Step Execution

Choose your learning style10 modes available
Process Flow - Multiple Compose files (override)
Start with base docker-compose.yml
↓
Read override docker-compose.override.yml
↓
Merge override settings into base
↓
Final combined configuration
↓
Run docker-compose with merged config
Docker Compose reads the base file first, then merges settings from the override file, combining them to create the final configuration used to run containers.
Execution Sample
Docker
version: '3.8'
services:
  web:
    image: nginx:alpine
    ports:
      - "80:80"
Base docker-compose.yml defines a web service running nginx on port 80.
Process Table
StepFile ReadActionConfiguration StateResulting Service Config
1docker-compose.ymlLoad base configweb service with image nginx:alpine and ports 80:80web: image=nginx:alpine, ports=[80:80]
2docker-compose.override.ymlLoad override configoverride web service with environment and portsweb: environment=[DEBUG=1], ports=[8080:80]
3Merge override into baseOverride ports and add environmentweb service with image nginx:alpine, ports 8080:80, environment DEBUG=1web: image=nginx:alpine, ports=[8080:80], environment=[DEBUG=1]
4Run docker-compose upUse merged configContainers start with final configweb container runs nginx with port 8080 mapped and DEBUG=1 env
5ExitProcess completeFinal config appliedContainers running as expected
💡 All compose files merged; docker-compose runs containers with combined settings.
Status Tracker
VariableStartAfter Step 2After Step 3Final
web.imagenginx:alpinenginx:alpinenginx:alpinenginx:alpine
web.ports[80:80][8080:80][8080:80][8080:80]
web.environmentnone[DEBUG=1][DEBUG=1][DEBUG=1]
Key Moments - 3 Insights
Why does the port mapping change from 80:80 to 8080:80 after merging?
The override file redefines the ports key, replacing the base ports. See execution_table step 3 where ports are overridden.
Does the override file replace or add to the base configuration?
It merges by replacing keys like ports but adds new keys like environment. Step 3 shows ports replaced and environment added.
What happens if a key is only in the base file and not in the override?
That key stays unchanged in the final config. For example, image remains nginx:alpine as override does not change it.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3. What is the final port mapping for the web service?
A80:80
B80:8080
C8080:80
DNo ports mapped
💡 Hint
Check the 'Resulting Service Config' column at step 3 in execution_table.
At which step does the environment variable DEBUG=1 get added to the web service?
AStep 2
BStep 1
CStep 3
DStep 4
💡 Hint
Look at the 'Configuration State' column for environment variables in execution_table.
If the override file did not specify ports, what would be the final port mapping?
ANo ports mapped
B80:80
C8080:80
DBoth 80:80 and 8080:80
💡 Hint
Refer to variable_tracker for web.ports values and how override affects them.
Concept Snapshot
Docker Compose reads multiple files in order.
The override file merges into the base.
Keys in override replace or add to base keys.
Final config combines both for container run.
Use 'docker-compose -f base -f override up' to apply.
Full Transcript
Docker Compose uses multiple files to configure containers. First, it reads the base file, then reads the override file. The override file merges its settings into the base, replacing or adding keys. For example, ports can be replaced, and environment variables can be added. The final combined configuration is used to start containers. This lets you customize or extend your setup easily without changing the base file.

Practice

(1/5)
1. What is the main purpose of using multiple Docker Compose files with the -f option?
easy
A. To override or extend settings without changing the original compose file
B. To run multiple containers simultaneously
C. To create multiple Docker images at once
D. To speed up Docker container startup time

Solution

  1. Step 1: Understand the role of multiple Compose files

    Multiple Compose files allow you to customize or override configurations without editing the original file.
  2. Step 2: Recognize the use of the -f option

    The -f option lets you specify multiple files, where later files override earlier ones.
  3. Final Answer:

    To override or extend settings without changing the original compose file -> Option A
  4. Quick Check:

    Multiple Compose files = override settings [OK]
Hint: Multiple files with -f override earlier settings [OK]
Common Mistakes:
  • Thinking multiple files run containers simultaneously
  • Confusing Compose files with Docker images
  • Assuming it speeds up container startup
2. Which of the following is the correct syntax to use two Docker Compose files named docker-compose.yml and docker-compose.override.yml together?
easy
A. docker-compose up -f docker-compose.yml -f docker-compose.override.yml
B. docker-compose -f docker-compose.yml -f docker-compose.override.yml up
C. docker-compose up docker-compose.yml docker-compose.override.yml
D. docker-compose -f up docker-compose.yml docker-compose.override.yml

Solution

  1. Step 1: Recall the correct order of options

    The -f option must come before the command and can be repeated for multiple files.
  2. Step 2: Verify the syntax correctness

    docker-compose -f docker-compose.yml -f docker-compose.override.yml up correctly places -f flags before the up command and lists both files.
  3. Final Answer:

    docker-compose -f docker-compose.yml -f docker-compose.override.yml up -> Option B
  4. Quick Check:

    Correct order: docker-compose -f file1 -f file2 up [OK]
Hint: Place all -f options before the command [OK]
Common Mistakes:
  • Putting -f after the command
  • Listing files without -f
  • Mixing command and file order
3. Given these two Compose files:
docker-compose.yml:
services:
  web:
    image: nginx:1.19
    ports:
      - "80:80"

docker-compose.override.yml:
services:
  web:
    image: nginx:1.21
    environment:
      - DEBUG=true

What image version will the web service use when running docker-compose -f docker-compose.yml -f docker-compose.override.yml up?
medium
A. nginx:1.19
B. nginx:latest
C. It will cause an error due to conflicting images
D. nginx:1.21

Solution

  1. Step 1: Understand override behavior

    The second file overrides matching keys from the first file, including the image version.
  2. Step 2: Identify the image in the override file

    The override file sets image: nginx:1.21, which replaces nginx:1.19.
  3. Final Answer:

    nginx:1.21 -> Option D
  4. Quick Check:

    Override file image replaces base image [OK]
Hint: Later files override earlier image settings [OK]
Common Mistakes:
  • Assuming base file image stays unchanged
  • Thinking it defaults to latest
  • Expecting an error from override
4. You run docker-compose -f base.yml -f override.yml up but get an error: ERROR: Service 'app' has both 'build' and 'image' specified. What is the likely cause?
medium
A. Both files define 'app' service with conflicting 'build' and 'image' keys
B. You forgot to specify the -d flag
C. The override file is missing the 'app' service definition
D. Docker Compose does not support multiple files

Solution

  1. Step 1: Understand the error message

    The error says the 'app' service has both 'build' and 'image' keys, which Docker Compose disallows.
  2. Step 2: Identify conflict from multiple files

    When combining files, if one file sets 'build' and another sets 'image' for the same service, this conflict occurs.
  3. Final Answer:

    Both files define 'app' service with conflicting 'build' and 'image' keys -> Option A
  4. Quick Check:

    Conflicting keys in combined files cause this error [OK]
Hint: Check for conflicting keys in combined service definitions [OK]
Common Mistakes:
  • Thinking -d flag fixes this error
  • Assuming missing service causes error
  • Believing multiple files are unsupported
5. You have a base Compose file docker-compose.yml and an override docker-compose.prod.yml that changes the web service's environment variables and ports. You want to run the production setup but keep the base file unchanged. Which command correctly applies the override and ensures the web service uses the production settings?
hard
A. docker-compose up -f docker-compose.yml -f docker-compose.prod.yml -d
B. docker-compose -f docker-compose.prod.yml -f docker-compose.yml up -d
C. docker-compose -f docker-compose.yml -f docker-compose.prod.yml up -d
D. docker-compose up -d -f docker-compose.yml -f docker-compose.prod.yml

Solution

  1. Step 1: Recall override order

    The last file listed with -f overrides earlier files.
  2. Step 2: Check command syntax and order

    docker-compose -f docker-compose.yml -f docker-compose.prod.yml up -d correctly lists base file first, then production override, followed by the up -d command.
  3. Final Answer:

    docker-compose -f docker-compose.yml -f docker-compose.prod.yml up -d -> Option C
  4. Quick Check:

    Last -f file overrides earlier ones [OK]
Hint: List base file first, override file second with -f [OK]
Common Mistakes:
  • Reversing file order so base overrides override
  • Placing -f after the command
  • Omitting -d for detached mode
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard