Recall & Review
beginner
What is the purpose of using multiple stages in a Dockerfile?
Multiple stages help separate the build environment from the final runtime environment. This keeps the final image small and secure by copying only needed files from the build stage.
Click to reveal answer
beginner
How do you copy files from a build stage to the final stage in a Dockerfile?
Use the
COPY --from=<stage-name> <source> <destination> command to copy files from a named build stage to the current stage.Click to reveal answer
beginner
What does the
--from flag do in the COPY command?The
--from flag tells Docker to copy files from a previous build stage instead of the local build context.Click to reveal answer
intermediate
Why is it beneficial to copy only specific files from the build stage to the final stage?
Copying only specific files reduces the final image size, improves security by excluding build tools, and speeds up deployment.
Click to reveal answer
intermediate
Give an example of a simple multi-stage Dockerfile copying a binary from build to final stage.
Example:
FROM golang:1.20 AS build WORKDIR /app COPY . . RUN go build -o myapp FROM alpine:latest COPY --from=build /app/myapp /usr/local/bin/myapp CMD ["/usr/local/bin/myapp"]
Click to reveal answer
What does the
--from option in the COPY command specify?✗ Incorrect
The
--from option specifies which build stage to copy files from in a multi-stage Dockerfile.Why use multi-stage builds in Docker?
✗ Incorrect
Multi-stage builds allow copying only necessary files to the final image, reducing size and improving security.
In a multi-stage Dockerfile, what happens if you omit the
--from flag in COPY?✗ Incorrect
Without
--from, COPY copies files from the local build context (your project folder).Which command copies a file named
app from a build stage named builder to the final image?✗ Incorrect
The correct syntax is
COPY --from=builder /app /app to copy from the build stage named 'builder'.What is a key benefit of separating build and final stages in Docker?
✗ Incorrect
Separating build and final stages keeps the final image small and free of unnecessary build tools.
Explain how to copy files from a build stage to the final stage in a Dockerfile and why this is useful.
Think about separating build environment from runtime environment.
You got /4 concepts.
Describe a simple example of a multi-stage Dockerfile that builds a binary and copies it to a smaller final image.
Use a language like Go or C for the example.
You got /4 concepts.