Bird
0
0

You want to build a minimal container with a Go app using scratch. Which Dockerfile snippet correctly builds and runs the app?

hard📝 Application Q8 of 15
Docker - Image Optimization
You want to build a minimal container with a Go app using scratch. Which Dockerfile snippet correctly builds and runs the app?
FROM golang:1.20 AS builder
WORKDIR /app
COPY . .
RUN go build -o myapp .

FROM scratch
COPY --from=builder /app/myapp /
CMD ["/myapp"]
AThis multi-stage build compiles a static binary and runs it on scratch
BThis will fail because scratch cannot copy from builder
CThe CMD should be a shell command to run myapp
DThe Go app must be built inside scratch
Step-by-Step Solution
Solution:
  1. Step 1: Understand multi-stage build

    The first stage builds the Go app with all tools, producing a binary.
  2. Step 2: Use scratch for minimal runtime

    The second stage copies the binary into scratch and runs it directly, which is correct.
  3. Final Answer:

    This multi-stage build compiles a static binary and runs it on scratch -> Option A
  4. Quick Check:

    Multi-stage build + scratch = minimal static binary container [OK]
Quick Trick: Use multi-stage build to get static binary for scratch [OK]
Common Mistakes:
  • Thinking scratch can build binaries
  • Trying to run shell commands in scratch

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Docker Quizzes