A multi-stage build is a Docker technique that uses multiple FROM statements in one Dockerfile to create smaller, optimized images by separating build and runtime environments.
Click to reveal answer
beginner
Why use multi-stage builds for static sites?
Multi-stage builds help keep the final image small by only including the static files needed to serve the site, removing build tools and dependencies.
Click to reveal answer
intermediate
In a multi-stage build for a static site, what is typically done in the first stage?
The first stage usually installs dependencies and builds the static site files using tools like npm or yarn.
Click to reveal answer
beginner
What role does nginx play in multi-stage builds for static sites?
Nginx serves the static files in the final stage, providing a lightweight and fast web server environment.
Click to reveal answer
intermediate
How does a multi-stage Dockerfile improve security for static sites?
By excluding build tools and source code from the final image, it reduces the attack surface and potential vulnerabilities.
Click to reveal answer
What is the main benefit of using multi-stage builds for static sites?
ASmaller final image size
BFaster build times
CMore layers in the image
DIncludes all build tools in the final image
✗ Incorrect
Multi-stage builds reduce the final image size by only copying necessary files to the last stage.
Which tool is commonly used to serve static sites in the final stage of a multi-stage build?
AApache
BNode.js
CNginx
DMySQL
✗ Incorrect
Nginx is a lightweight web server often used to serve static files efficiently.
In a multi-stage Dockerfile, what does the first stage usually contain?
AFinal runtime environment
BStatic files only
CNginx configuration
DBuild tools and source code
✗ Incorrect
The first stage installs dependencies and builds the static site.
How does multi-stage building affect security?
AIncreases attack surface
BReduces vulnerabilities by excluding build tools
CIncludes all source code in the final image
DRequires more permissions
✗ Incorrect
Excluding build tools and source code reduces the attack surface.
Which Dockerfile instruction is used to copy files from one stage to another?
ACOPY --from
BRUN
CADD
DENV
✗ Incorrect
COPY --from allows copying files from a previous build stage.
Explain how a multi-stage Docker build works for creating a static site with nginx.
Think about separating build and runtime environments.
You got /5 concepts.
Describe the advantages of using multi-stage builds for static sites in terms of performance and security.
Consider what is included and excluded in the final image.
You got /5 concepts.
Practice
(1/5)
1. What is the main benefit of using multi-stage builds for static sites with nginx?
easy
A. It automatically updates static files without rebuilding the image.
B. It allows running multiple nginx servers in one container.
C. It reduces the final image size by separating build and serve stages.
D. It enables dynamic content generation inside the nginx container.
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 C
Quick Check:
Multi-stage builds = smaller images [OK]
Hint: Remember: build tools stay out of final image for smaller size [OK]
Common Mistakes:
Thinking multi-stage runs multiple nginx servers
Believing static files update automatically without rebuild
Confusing static site builds with dynamic content generation
2. Which Dockerfile snippet correctly copies static files from a builder stage to the nginx stage?
easy
A. COPY --to=builder /app/dist /usr/share/nginx/html
B. COPY /app/dist /usr/share/nginx/html --from=builder
C. COPY /usr/share/nginx/html /app/dist --from=builder
D. COPY --from=builder /app/dist /usr/share/nginx/html
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 D
Quick Check:
COPY --from=stage source dest = correct [OK]
Hint: COPY uses --from=stage before source path [OK]
Common Mistakes:
Placing --from after paths
Using --to instead of --from
Reversing source and destination paths
3. Given this Dockerfile snippet, what will be the size impact on the final image?
FROM node:18 AS builder
WORKDIR /app
COPY . .
RUN npm run build
FROM nginx:alpine
COPY --from=builder /app/dist /usr/share/nginx/html
medium
A. Final image will be small because only nginx and static files are included.
B. Final image will be empty because build files are not copied.
C. Final image will be large because node is included.
D. Final image will contain both node and nginx layers.
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 A