wasAsked.

What is a multi-stage Docker build and why is it used?

Frequently Asked (All Companies) · Role not specified · technical

Published

Year not specified · Docker, DevOps

Community answers

Posted by wasAsked editorial collection

A multi-stage Docker build uses several FROM statements in one Dockerfile. Each FROM starts a new stage. You build the application in an earlier stage with full tooling, then copy only the final output into a small runtime stage. Everything not copied is discarded from the final image. Example: React app served by NGINX # Stage 1: build FROM node:22-alpine AS build WORKDIR /app COPY package.json package-lock.json ./ RUN npm ci COPY . . RUN npm run build # Stage 2: runtime FROM nginx:1.27-alpine COPY --from=build /app/dist /usr/share/nginx/html COPY nginx.conf /etc/nginx/conf.d/default.conf EXPOSE 80 The final image contains NGINX and static files only — no Node.js, no node_modules and no source code. Example: Spring Boot application FROM eclipse-temurin:21-jdk AS build WORKDIR /src COPY . . RUN ./mvnw -q package -DskipTests FROM eclipse-temurin:21-jre WORKDIR /app COPY --from=build /src/target/app.jar app.jar USER 1000 ENTRYPOINT ["java", "-jar", "app.jar"] Why multi-stage builds are used 1. Smaller images: SDKs, compilers and build dependencies stay in the build stage. A .NET SDK image is several hundred megabytes larger than the ASP.NET runtime image. Smaller images pull faster, start faster and cost less to store. 2. Better security: fewer packages means a smaller attack surface and fewer vulnerabilities; source code and build secrets do not end up in the production image. 3. Single Dockerfile: no need for separate build scripts or a "builder" image process; the whole build is reproducible in one file. 4. Build caching: stages are cached independently. Restoring dependencies in a separate layer means code changes do not re-download packages. 5. Flexibility: you can add extra stages for testing or linting and target them explicitly: docker build --target test . Stages that are not needed for the target are skipped by BuildKit, and independent stages build in parallel. Useful techniques - Name stages with AS (build, test, final) and copy with COPY --from=build. - Copy from external images too: COPY --from=busybox:1.36 /bin/wget /usr/local/bin/wget. - Use build cache mounts to speed up package restores: RUN --mount=type=cache,target=/root/.nuget/packages dotnet restore. - Use secret mounts for private package feeds: RUN --mount=type=secret,id=nugetconfig ... so credentials never enter image layers. - Choose minimal final bases: distroless, Alpine, or Microsoft's chiseled Ubuntu images for .NET. Before multi-stage builds, teams used a "builder pattern" with two Dockerfiles and shell scripts to copy artifacts between images; multi-stage builds made that unnecessary. Interview summary: "A multi-stage build compiles the app in a stage with full tooling and copies only the output into a slim runtime stage, producing smaller, more secure images from a single Dockerfile, with better caching."

Ravindra Wadile · · 0 likes

Sign in to add your answer

Related interview topics