Docker Interview Question 3: Multi Stage Small Image
Docker Interview Questions · Test 1 · question 3 of 10
A Go service is built with FROM golang:1.26, then COPY . . and RUN go build -o /bin/app. The resulting image is close to 1 GB because it contains the whole Go toolchain, although the service only needs the compiled binary at runtime. You want a much smaller image and still a single Dockerfile built with a plain docker build. What should you do?
Show answer and explanation
Answer:
In a multi-stage build each FROM starts a new stage, and you can selectively copy artifacts from one stage to another, leaving behind everything you don't want in the final image. The build stage has the compiler; the final stage (for example scratch or a slim base) receives only the binary via COPY --from, so the toolchain never ends up in the shipped image. It is still one Dockerfile and one docker build.
Option B is wrong because docker image prune removes dangling or unused *images*; it does not strip files out of an image's layers.
Option C misunderstands where the toolchain comes from. It is part of the golang base image, not the build context, so .dockerignore cannot exclude it.
Option D changes only what runs at startup. The toolchain's files are still in the image layers, so the image stays the same size.
Reference: Multi-stage builds