Using Multiple Docker Compose Files to Override Settings
๐ Scenario: You are working on a web application that uses Docker Compose to run a web server and a database. You want to create a base Compose file for the common setup and an override Compose file to change the web server's port for development.
๐ฏ Goal: Learn how to create and use multiple Docker Compose files to override service settings without changing the base configuration.
๐ What You'll Learn
Create a base Docker Compose file named docker-compose.yml with a web and db service
Create an override Docker Compose file named docker-compose.override.yml to change the web service port
Use the docker compose command to start the services with the override applied
Verify the web service is running on the new port
๐ก Why This Matters
๐ Real World
In real projects, multiple Compose files let developers keep a stable base setup and customize it for different environments like development, testing, or production without duplicating all settings.
๐ผ Career
Understanding how to use multiple Docker Compose files is important for DevOps roles to manage containerized applications efficiently and maintain clean, reusable configurations.
Progress0 / 4 steps
1
Create the base Docker Compose file
Create a file named docker-compose.yml with two services: web using the image nginx:alpine and db using the image postgres:alpine. Set the web service to expose port 80.
Docker
Hint
Use the services key to define both web and db services with their images and ports.
2
Create the override Docker Compose file
Create a file named docker-compose.override.yml that overrides the web service port to 8080 instead of 80.
Docker
Hint
Only include the web service and override the ports key to map port 8080 on the host to port 80 in the container.
3
Start the services using both Compose files
Run the docker compose command to start the services using both docker-compose.yml and docker-compose.override.yml files together.
Docker
Hint
Use docker compose up -d to start the services in detached mode. Docker Compose automatically uses both files if named docker-compose.yml and docker-compose.override.yml.
4
Verify the web service is running on port 8080
Run the command docker compose ps and check that the web service is mapped to port 8080 on the host.
Docker
Hint
Use docker compose ps to list running containers and their port mappings. Look for 0.0.0.0:8080->80/tcp under the web service.
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
Step 1: Understand the role of multiple Compose files
Multiple Compose files allow you to customize or override configurations without editing the original file.
Step 2: Recognize the use of the -f option
The -f option lets you specify multiple files, where later files override earlier ones.
Final Answer:
To override or extend settings without changing the original compose file -> Option A
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
Step 1: Recall the correct order of options
The -f option must come before the command and can be repeated for multiple files.
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.
Final Answer:
docker-compose -f docker-compose.yml -f docker-compose.override.yml up -> Option B
Quick Check:
Correct order: docker-compose -f file1 -f file2 up [OK]
Hint: Place all -f options before the command [OK]
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
Step 1: Understand override behavior
The second file overrides matching keys from the first file, including the image version.
Step 2: Identify the image in the override file
The override file sets image: nginx:1.21, which replaces nginx:1.19.
Final Answer:
nginx:1.21 -> Option D
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
Step 1: Understand the error message
The error says the 'app' service has both 'build' and 'image' keys, which Docker Compose disallows.
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.
Final Answer:
Both files define 'app' service with conflicting 'build' and 'image' keys -> Option A
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
Step 1: Recall override order
The last file listed with -f overrides earlier files.
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.
Final Answer:
docker-compose -f docker-compose.yml -f docker-compose.prod.yml up -d -> Option C
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 unknownstrongwhile earning +1 Grip toward your certificate.