Building images in Compose in Docker - Time & Space Complexity
When building images using Docker Compose, it's important to understand how the build time grows as the project size increases.
We want to know how the time to build images changes when we add more services or larger Dockerfiles.
Analyze the time complexity of this Docker Compose build snippet.
version: '3.8'
services:
app:
build: ./app
db:
build: ./db
cache:
build: ./cache
This Compose file builds three images from three different directories.
Look for repeated build steps or loops.
- Primary operation: Building each service's Docker image.
- How many times: Once per service listed (3 times here).
Each new service adds a full image build process.
| Input Size (n = number of services) | Approx. Operations (image builds) |
|---|---|
| 10 | 10 image builds |
| 100 | 100 image builds |
| 1000 | 1000 image builds |
Pattern observation: The total build time grows roughly in direct proportion to the number of services.
Time Complexity: O(n)
This means the build time increases linearly as you add more services to build.
[X] Wrong: "Building multiple images in Compose happens all at once, so time stays the same no matter how many services."
[OK] Correct: Each image build runs separately and takes time, so more services mean more total build time.
Understanding how build time scales helps you plan and optimize projects, a useful skill in real-world DevOps work.
What if we used a shared base image for all services? How would that affect the build time complexity?
Practice
build key do in a Docker Compose file?Solution
Step 1: Understand the role of
Thebuildin Composebuildkey tells Docker Compose to create an image from source files, usually by specifying a directory with a Dockerfile.Step 2: Compare with other keys
Other keys likeimagespecify existing images, andenvironmentsets variables, so they do not build images.Final Answer:
It tells Compose how to build a Docker image from a directory or Dockerfile. -> Option DQuick Check:
build= build image [OK]
- Confusing build with image key
- Thinking build runs containers
- Mixing build with environment variables
Solution
Step 1: Recall correct keys under build
The correct keys arecontextfor directory anddockerfilefor custom Dockerfile name.Step 2: Check each option
build: context: ./app dockerfile: Dockerfile.custom usescontextanddockerfilecorrectly. Others use invalid keys likepath,file, orsource.Final Answer:
build: context: ./app dockerfile: Dockerfile.custom -> Option CQuick Check:
Use context and dockerfile keys [OK]
- Using 'path' or 'file' instead of 'context' and 'dockerfile'
- Indentation errors under build
- Putting dockerfile outside build block
services:
web:
build:
context: ./webapp
image: mywebapp:latest
What happens when you run docker compose up --build?Solution
Step 1: Understand build and image keys together
Thebuildkey builds an image from the given context. Theimagekey tags the built image with the given name.Step 2: Effect of --build flag
The--buildflag forces Compose to build the image before starting containers.Final Answer:
It builds the image from ./webapp and tags it as mywebapp:latest, then starts the container. -> Option AQuick Check:
build + image = build and tag image [OK]
- Thinking image pulls from Docker Hub automatically
- Assuming build does not tag image
- Ignoring --build flag effect
services:
api:
build: ./api
dockerfile: Dockerfile.api
When running docker compose build, you get an error. What is wrong?Solution
Step 1: Check Compose syntax for build and dockerfile
Thedockerfilekey must be nested inside thebuilddictionary, not at the same level asbuild.Step 2: Identify the error cause
Here,buildis a string, anddockerfileis separate, causing Compose to fail parsing.Final Answer:
Thedockerfilekey must be inside thebuildblock, not at the same level. -> Option BQuick Check:
dockerfile inside build block [OK]
- Placing dockerfile outside build block
- Using build as string with dockerfile key separately
- Assuming dockerfile key is global
frontend and backend, each with different Dockerfiles in the same directory ./app. How do you configure the build section for both services correctly?Solution
Step 1: Understand build context and dockerfile usage
You can use the samecontextdirectory but specify differentdockerfilenames to build different images from the same folder.Step 2: Check options for multiple services
Usecontext: ./appfor both, and specify differentdockerfilenames under each service's build. correctly uses the same context with different Dockerfiles. Use differentcontextpaths for each service pointing to the same directory. is redundant, C limits building, and D is false.Final Answer:
Usecontext: ./appfor both, and specify differentdockerfilenames under each service's build. -> Option AQuick Check:
Same context + different dockerfile names [OK]
- Trying to use different contexts for same folder
- Thinking multiple Dockerfiles in one folder is unsupported
- Not specifying dockerfile key for each service
Ready to prove it?
Pass the test and this pattern moves unknown
