Interview Prep/DevSecOps/Container and IaC Security

Why and how do you use multi-stage builds from a security point of view?

IntermediateScenarioContainer and IaC Security
Answer

Situation read → Build tools, source code and caches in the final image add CVEs and help an attacker.

What I'd do →

  1. Build in one stage, copy only the artifact to a minimal final stage:
FROM golang:1.23 AS build
WORKDIR /src
COPY . .
RUN CGO_ENABLED=0 go build -o /out/app ./cmd/app

FROM gcr.io/distroless/static-debian12:nonroot
COPY --from=build /out/app /app
ENTRYPOINT ["/app"]
  1. Pin base images by digest.
  2. Keep .dockerignore strict (.git, .env, *.pem).

Why → The final image contains only what runs.

Interview tip → Mention .dockerignore as a common source of leaked files.

All DevSecOps questions Roadmap: Policy as code

More on Container and IaC Security