Builder pattern before multi-stage in Docker - Time & Space Complexity
We want to understand how the time to build a Docker image grows when using the builder pattern before multi-stage builds.
Specifically, how the build steps affect the total build time as the project size increases.
Analyze the time complexity of the following Dockerfile snippet.
FROM node:18 AS builder
WORKDIR /app
COPY package.json .
RUN npm install
COPY . .
RUN npm run build
FROM nginx:alpine
COPY --from=builder /app/build /usr/share/nginx/html
This Dockerfile uses a builder stage to install dependencies and build the app, then copies the build output to a smaller image.
Look for repeated or costly steps in the build process.
- Primary operation: Running
npm installandnpm run buildwhich process all project files. - How many times: Each command runs once per build, but their cost depends on project size.
As the project size (number of files and dependencies) grows, the install and build steps take longer.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 files | Small install and build time |
| 100 files | Much longer install and build time |
| 1000 files | Significantly longer install and build time |
Pattern observation: Build time grows roughly proportional to project size because install and build process all files.
Time Complexity: O(n)
This means the build time grows roughly in direct proportion to the size of the project files and dependencies.
[X] Wrong: "The build time stays the same no matter how big the project is."
[OK] Correct: The install and build commands process all files and dependencies, so more files mean more work and longer build time.
Understanding how build steps scale with project size helps you explain Docker build performance and optimization in real projects.
What if we split the build into multiple smaller stages? How would that affect the time complexity?
Practice
Solution
Step 1: Understand the builder pattern concept
The builder pattern splits the process into two parts: building the app and running it.Step 2: Identify the benefit of separation
This separation keeps the final image small by excluding build tools and files.Final Answer:
To separate the build environment from the final runtime environment -> Option BQuick Check:
Builder pattern = separate build and run [OK]
- Confusing builder pattern with running multiple containers
- Thinking it manages volumes or updates automatically
- Assuming it is about Docker networking
Solution
Step 1: Identify builder pattern before multi-stage
Before multi-stage, builder pattern used separate Dockerfiles or manual copying, not the --from syntax.Step 2: Check each option
FROM node:14 AS builder WORKDIR /app COPY . . RUN npm install && npm run build FROM nginx:alpine COPY --from=builder /app/build /usr/share/nginx/html uses multi-stage syntax (--from=builder), which was not available before multi-stage builds.Final Answer:
FROM node:14 WORKDIR /app COPY . . RUN npm install && npm run build CMD ["npm", "start"] -> Option AQuick Check:
Before multi-stage, no --from usage [OK]
- Choosing multi-stage syntax before it existed
- Confusing build and runtime commands
- Ignoring the CMD instruction
FROM node:14 WORKDIR /app COPY . . RUN npm install && npm run build CMD ["node", "server.js"]
What is the likely size impact on the final image?
Solution
Step 1: Analyze the Dockerfile steps
The Dockerfile copies all source files and runs build inside the same image.Step 2: Understand image size impact
Since build tools and source files remain, the image size is larger than needed.Final Answer:
The image will be large because build tools and source files remain -> Option AQuick Check:
Single-stage build keeps all files, so image is large [OK]
- Assuming build output only is copied
- Thinking Docker cleans up build files automatically
- Ignoring the COPY . . step
Solution
Step 1: Understand docker cp syntax
The correct syntax is 'docker cp <container>:<source_path> <destination_path>'.Step 2: Match command to copy from container to local
docker cp builder:/app/build ./build correctly copies from container 'builder' path to local './build'.Final Answer:
docker cp builder:/app/build ./build -> Option CQuick Check:
docker cp container:path local_path [OK]
- Using 'docker copy' instead of 'docker cp'
- Reversing source and destination paths
- Trying to copy from local to container incorrectly
Dockerfile.build and Dockerfile.run. Which sequence of commands correctly builds and prepares the final image with only the build output copied?Solution
Step 1: Build the builder image and create a container
Use 'docker build' with Dockerfile.build, then 'docker create' to make a stopped container for copying files.Step 2: Copy build output and build runtime image
Copy files from the builder container, remove it, then build the runtime image with Dockerfile.run.Final Answer:
docker build -f Dockerfile.build -t app-builder . && docker create --name builder app-builder && docker cp builder:/app/dist ./dist && docker rm builder && docker build -f Dockerfile.run -t app-runtime . -> Option DQuick Check:
Build, create, copy, remove, then build runtime [OK]
- Using 'docker run' instead of 'docker create' for copying
- Building runtime image before copying build output
- Not removing temporary builder container
Ready to prove it?
Pass the test and this pattern moves unknown
