A multi-stage Docker build uses multiple FROM statements in one Dockerfile to create intermediate images. This helps build smaller final images by copying only needed files from earlier stages.
Click to reveal answer
beginner
Why use multi-stage builds for different environments?
Multi-stage builds let you create separate stages for development, testing, and production in one Dockerfile. This keeps images optimized and environment-specific without repeating code.
Click to reveal answer
intermediate
How do you specify which stage to use when building a Docker image?
Use the --target option with docker build to specify the stage name you want to build. For example: docker build --target production .
Click to reveal answer
intermediate
What is the benefit of copying files from one stage to another in a multi-stage build?
Copying files from one stage to another lets you separate build dependencies from runtime files. This reduces the final image size and improves security by excluding build tools.
Click to reveal answer
intermediate
Give an example of a simple multi-stage Dockerfile for development and production.
Example:
FROM node:18 AS development
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
FROM node:18 AS production
WORKDIR /app
COPY --from=development /app .
RUN npm prune --production
CMD ["node", "server.js"]
Click to reveal answer
What does the --target option do in a multi-stage Docker build?
ASpecifies which build stage to build
BSets the base image for the build
CDefines the output image name
DRuns the container after build
✗ Incorrect
The --target option tells Docker which stage in the Dockerfile to build, allowing you to build only a specific environment stage.
Why are multi-stage builds useful for production images?
AThey run faster on all machines
BThey include all development tools
CThey create smaller, optimized images
DThey automatically update dependencies
✗ Incorrect
Multi-stage builds help create smaller images by excluding unnecessary build tools and files, making production images optimized.
In a multi-stage Dockerfile, how do you copy files from a previous stage?
ARUN copy stage_name source destination
BADD --stage=stage_name source destination
CCOPY source destination
DCOPY --from=stage_name source destination
✗ Incorrect
The syntax COPY --from=stage_name copies files from a named build stage to the current stage.
Which of these is NOT a benefit of multi-stage builds?
ASeparate build and runtime environments
BSimplifies Dockerfile by combining all steps
CSmaller final image size
DImproves security by excluding build tools
✗ Incorrect
Multi-stage builds can make Dockerfiles longer but more organized; they do not necessarily simplify by combining all steps.
What base image is commonly used for Node.js multi-stage builds?
Anode:18
Bpython:3.12
Cubuntu:latest
Dalpine:latest
✗ Incorrect
The node:18 image is a common official Node.js base image used in multi-stage builds for Node.js apps.
Explain how multi-stage Docker builds help manage different environments like development and production.
Think about how you can build once but get different images for dev and prod.
You got /5 concepts.
Describe the steps to create a multi-stage Dockerfile for a Node.js app with development and production stages.
Focus on what happens in each stage and how they connect.
You got /6 concepts.
Practice
(1/5)
1. What is the main purpose of using multi-stage builds in Docker for different environments?
easy
A. To automatically update Docker images from the internet
B. To run multiple containers simultaneously
C. To create smaller and cleaner Docker images by separating build and runtime stages
D. To increase the size of the Docker image for better performance
Solution
Step 1: Understand multi-stage build concept
Multi-stage builds separate the build environment from the runtime environment to avoid including unnecessary build tools in the final image.
Step 2: Identify the benefit for different environments
This separation results in smaller, cleaner images optimized for the target environment, such as production.
Final Answer:
To create smaller and cleaner Docker images by separating build and runtime stages -> Option C
Quick Check:
Multi-stage builds = smaller images [OK]
Hint: Remember: multi-stage means build then runtime separate [OK]
Common Mistakes:
Thinking multi-stage runs multiple containers
Believing it automatically updates images
Assuming it increases image size
2. Which of the following is the correct syntax to name a build stage in a Dockerfile?
easy
A. STAGE node:18 build
B. FROM node:18 AS build
C. BUILD node:18 AS build
D. FROM node:18 WITH build
Solution
Step 1: Recall Dockerfile stage naming syntax
Docker uses the syntax 'FROM <image> AS ' to name a build stage.
Step 2: Check each option
Only FROM node:18 AS build uses the correct 'FROM ... AS ...' syntax; others are invalid Dockerfile commands.
Final Answer:
FROM node:18 AS build -> Option B
Quick Check:
Stage naming = FROM ... AS ... [OK]
Hint: Look for 'FROM ... AS ...' to name stages [OK]
Common Mistakes:
Using 'STAGE' or 'BUILD' keywords incorrectly
Forgetting the AS keyword
Using 'WITH' instead of AS
3. Given this Dockerfile snippet, what will be the size characteristic of the final image?
FROM node:18 AS build
RUN npm install
FROM node:18-slim
COPY --from=build /app/node_modules /app/node_modules
medium
A. The final image will be smaller because it only copies needed files from build stage
B. The final image will be the same size as the build stage
C. The final image will fail to build due to missing files
D. The final image will be large because it includes all build tools
Solution
Step 1: Analyze the multi-stage build steps
The first stage installs dependencies with full Node image; the second stage uses a smaller Node image and copies only node_modules from the build stage.
Step 2: Understand image size impact
Because only necessary files are copied, the final image is smaller than the full build environment image.
Final Answer:
The final image will be smaller because it only copies needed files from build stage -> Option A
Quick Check:
Copying from build stage reduces final image size [OK]
Hint: Copy only needed files from build stage to keep image small [OK]
Common Mistakes:
Assuming all build tools are included in final image
Thinking the build will fail due to missing files
Believing final image equals build stage size
4. Identify the error in this Dockerfile snippet for multi-stage build:
FROM python:3.12 AS build
RUN pip install -r requirements.txt
FROM python:3.12-slim
COPY --from=builder /app /app
medium
A. The COPY command syntax is incorrect
B. The requirements.txt file is missing
C. The base images are incompatible
D. The stage name 'builder' does not match the first stage name
Solution
Step 1: Check stage names consistency
The first stage is named 'build' but COPY uses '--from=builder', which does not exist.
Step 2: Verify COPY command correctness
The COPY syntax is correct, but the source stage name is wrong, causing build failure.
Final Answer:
The stage name 'builder' does not match the first stage name -> Option D
Quick Check:
Stage names must match exactly in COPY --from= [OK]
Hint: Match stage names exactly in COPY --from= [OK]
Common Mistakes:
Using wrong stage name in COPY
Confusing syntax errors with naming errors
Assuming base images cause this error
5. You want to build a Docker image that uses a heavy build environment only during build, but produces a minimal runtime image for production. Which multi-stage Dockerfile snippet correctly achieves this?
hard
A. FROM golang:1.20 AS build
WORKDIR /app
RUN go build -o app .
FROM alpine:latest
COPY --from=build /app/app /app/app
CMD ["/app/app"]
B. FROM alpine:latest AS build
RUN apk add go
WORKDIR /app
RUN go build -o app .
FROM golang:1.20
COPY --from=build /app/app /app/app
CMD ["/app/app"]
C. FROM golang:1.20
WORKDIR /app
RUN go build -o app .
CMD ["/app/app"]
D. FROM alpine:latest
RUN apk add go
WORKDIR /app
RUN go build -o app .
CMD ["/app/app"]
Solution
Step 1: Identify build and runtime separation
FROM golang:1.20 AS build
WORKDIR /app
RUN go build -o app .
FROM alpine:latest
COPY --from=build /app/app /app/app
CMD ["/app/app"] uses golang image for building, then copies only the built app to a minimal alpine image for runtime.
Step 2: Check other options for correctness
FROM alpine:latest AS build
RUN apk add go
WORKDIR /app
RUN go build -o app .
FROM golang:1.20
COPY --from=build /app/app /app/app
CMD ["/app/app"] builds in alpine but runs in golang image, which is opposite of desired minimal runtime. Options C and D do not separate build and runtime stages.
Final Answer:
Option A correctly uses multi-stage build for minimal runtime image -> Option A
Quick Check:
Build heavy, run minimal = FROM golang:1.20 AS build
WORKDIR /app
RUN go build -o app .
FROM alpine:latest
COPY --from=build /app/app /app/app
CMD ["/app/app"] [OK]
Hint: Build in heavy image, copy to minimal runtime image [OK]
Common Mistakes:
Building in minimal image causing slow builds
Running heavy image in production
Not separating build and runtime stages
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.