Multi-stage builds for static sites in Nginx - Time & Space Complexity
We want to understand how the time it takes to build and serve a static site changes as the site grows.
How does the build process scale when we add more files or steps?
Analyze the time complexity of the following nginx multi-stage build snippet.
# Stage 1: Build static files
FROM node:18 AS builder
WORKDIR /app
COPY package.json .
RUN npm install
COPY . .
RUN npm run build
# Stage 2: Serve with nginx
FROM nginx:stable-alpine
COPY --from=builder /app/dist /usr/share/nginx/html
This snippet builds a static site using Node.js, then copies the built files to an nginx server image to serve them.
Look for repeated steps or loops in the build and serve process.
- Primary operation: npm install and npm run build process that reads and processes all source files.
- How many times: Each source file is processed once during the build stage.
As the number of source files grows, the build step takes longer because it processes each file.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 files | Processes 10 files once |
| 100 files | Processes 100 files once |
| 1000 files | Processes 1000 files once |
Pattern observation: The build time grows roughly in direct proportion to the number of files.
Time Complexity: O(n)
This means the build time grows linearly as you add more files to your static site.
[X] Wrong: "Adding more files won't affect build time much because nginx just serves files quickly."
[OK] Correct: The build step processes every file, so more files mean more work before nginx can serve them.
Understanding how build steps scale helps you explain deployment choices and predict build times in real projects.
What if we added caching to the npm install step? How would the time complexity change?
Practice
Solution
Step 1: Understand multi-stage build purpose
Multi-stage builds separate the build process from the final image, so only necessary files are included.Step 2: Identify benefits for static sites
This separation reduces the final image size and improves security by excluding build tools from the runtime image.Final Answer:
It reduces the final image size by separating build and serve stages. -> Option CQuick Check:
Multi-stage builds = smaller images [OK]
- Thinking multi-stage runs multiple nginx servers
- Believing static files update automatically without rebuild
- Confusing static site builds with dynamic content generation
Solution
Step 1: Recall correct COPY syntax for multi-stage
The syntax is COPY --from=stage source_path destination_path.Step 2: Match snippet to syntax
COPY --from=builder /app/dist /usr/share/nginx/html uses COPY --from=builder /app/dist /usr/share/nginx/html which is correct.Final Answer:
COPY --from=builder /app/dist /usr/share/nginx/html -> Option DQuick Check:
COPY --from=stage source dest = correct [OK]
- Placing --from after paths
- Using --to instead of --from
- Reversing source and destination paths
FROM node:18 AS builder WORKDIR /app COPY . . RUN npm run build FROM nginx:alpine COPY --from=builder /app/dist /usr/share/nginx/html
Solution
Step 1: Analyze multi-stage build layers
The builder stage uses node to build files, but the final stage uses nginx:alpine only.Step 2: Understand what is copied to final image
Only the /app/dist folder from builder is copied into nginx image, so node and build tools are excluded.Final Answer:
Final image will be small because only nginx and static files are included. -> Option AQuick Check:
Multi-stage excludes build tools = smaller image [OK]
- Assuming node stays in final image
- Thinking build files are missing
- Believing both node and nginx layers combine
FROM node:18 AS builder WORKDIR /app COPY . . RUN npm run build FROM nginx:alpine COPY /app/dist /usr/share/nginx/htmlWhat is the error?
Solution
Step 1: Check COPY command in final stage
The COPY command lacks --from=builder, so it copies from the current stage which is empty.Step 2: Understand multi-stage copy syntax
To copy files from builder stage, COPY must use --from=builder to access that stage's files.Final Answer:
COPY command misses --from=builder to copy from the build stage. -> Option AQuick Check:
Missing --from=builder causes empty copy [OK]
- Forgetting --from=builder in COPY
- Assuming build files auto-copy without --from
- Blaming nginx image for missing files
Solution
Step 1: Understand multi-stage build best practices
Best practice is to separate build tools from runtime to keep images small and secure.Step 2: Choose approach minimizing layers and size
Using a builder stage to create static files, then copying only those files into a minimal nginx image achieves this.Final Answer:
Use a builder stage to create static files, then copy only those files into a minimal nginx image. -> Option BQuick Check:
Separate build and serve stages = optimized image [OK]
- Combining build and serve increases image size
- Installing build tools in nginx image adds unnecessary layers
- Copying entire project wastes space and layers
Ready to prove it?
Pass the test and this pattern moves unknown
