Bird
0
0

You want to build a Docker image for a Go application but keep it as small as possible. Which multi-stage Dockerfile snippet achieves this best?

hard📝 Best Practice Q15 of 15
Docker - Image Optimization
You want to build a Docker image for a Go application but keep it as small as possible. Which multi-stage Dockerfile snippet achieves this best?
FROM golang:1.20 AS builder
WORKDIR /app
COPY . .
RUN go build -o myapp

FROM alpine:latest
COPY --from=builder /app/myapp /usr/local/bin/myapp
CMD ["myapp"]
AThis build copies all source files to Alpine, increasing image size unnecessarily
BUsing Alpine as final image is bad because it lacks Go runtime
CThis multi-stage build compiles in golang image and copies only binary to Alpine, minimizing size
DYou should install Go in Alpine to build and run in one stage
Step-by-Step Solution
Solution:
  1. Step 1: Understand multi-stage build purpose

    Build stage uses full Go image; final stage uses minimal Alpine with only binary copied.
  2. Step 2: Analyze final image contents

    Only the compiled binary is copied, no source or build tools, keeping image small.
  3. Final Answer:

    This multi-stage build compiles in golang image and copies only binary to Alpine, minimizing size -> Option C
  4. Quick Check:

    Multi-stage build + minimal final image = small size [OK]
Quick Trick: Use multi-stage builds to copy only final artifacts [OK]
Common Mistakes:
  • Copying source files to final image
  • Installing build tools in final image
  • Building and running in same large image

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Docker Quizzes