0
0
Dockerdevops~10 mins

Builder pattern before multi-stage in Docker - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Builder pattern with multi-stage
Start: Write Dockerfile
Build full image with builder tools
Run build commands inside image
Copy build artifacts from builder
Create smaller runtime image
Copy artifacts into runtime image
Final image ready for deployment
This flow shows the builder pattern with multi-stage builds: build everything in the builder stage, then copy artifacts to a smaller runtime image.
Execution Sample
Docker
FROM node:14 as builder
WORKDIR /app
COPY package.json ./
RUN npm install
COPY . ./
RUN npm run build

FROM node:14-slim
WORKDIR /app
COPY --from=builder /app/build ./build
CMD ["node", "build/index.js"]
This Dockerfile builds the app in a full node image, then copies only the build output to a smaller runtime image.
Process Table
StepActionImage StateResult
1Start with 'node:14' as builderBuilder image createdReady to install dependencies
2Copy package.jsonpackage.json in /appFiles ready for npm install
3Run npm installnode_modules installedDependencies ready
4Copy source codeSource code in /appReady to build
5Run npm run buildBuild output in /app/buildBuild artifacts created
6Start new image 'node:14-slim'Clean runtime imageSmaller base image
7Copy build artifacts from builderBuild files in runtime imageOnly needed files copied
8Set CMD to run appRuntime image readyImage ready to run app
9Build completeFinal image createdReady for deployment
💡 All build steps done; final image contains only runtime files, no build tools
Status Tracker
VariableStartAfter Step 3After Step 5After Step 7Final
/app contentsemptypackage.json + node_modulespackage.json + node_modules + source + build outputbuild output onlybuild output only
Image sizebase sizelarger (with node_modules)even larger (with build tools)smaller (runtime only)smaller (runtime only)
Key Moments - 3 Insights
Why do we need two FROM lines in the Dockerfile?
Because the first FROM creates the builder image with all tools to build the app, and the second FROM creates a smaller runtime image. This is shown in execution_table steps 1 and 6.
How do build artifacts get from the builder image to the runtime image?
They are copied using COPY --from=builder, as shown in step 7 of the execution_table.
Why is the final image smaller than the builder image?
Because it only contains the build output and runtime dependencies, not the full build tools or source code, as shown by the variable_tracker for image size after step 7.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 5. What is present in the builder image?
ASource code, node_modules, and build output
BOnly build output
COnly source code and build output
DEmpty /app directory
💡 Hint
Check the 'Image State' column at step 5 in execution_table
At which step does the runtime image get the build artifacts copied in?
AStep 4
BStep 6
CStep 7
DStep 8
💡 Hint
Look for the COPY --from=builder action in execution_table
If we skipped step 3 (npm install), what would happen to the build output?
ABuild output would be created normally
BBuild would fail due to missing dependencies
CBuild output would be empty but image size same
DRuntime image would be larger
💡 Hint
Step 3 installs dependencies needed for build, check execution_table step 3
Concept Snapshot
Builder pattern with multi-stage uses multiple FROM lines.
First stage builds app with all tools.
Second stage is smaller runtime.
Build artifacts copied using --from.
Final image is smaller and cleaner.
Full Transcript
This visual execution shows how the builder pattern works with multi-stage Docker builds using multiple FROM lines. First, a full image with build tools is created. Then the app is built inside it. After that, a smaller runtime image is created. Build artifacts are copied from the builder image to the runtime image. This reduces final image size by excluding build tools. The execution table traces each step from starting the builder image, installing dependencies, building the app, to copying artifacts and setting the runtime command. The variable tracker shows how the /app directory contents and image size change after key steps. Key moments clarify why two FROM lines are needed, how copying works, and why the final image is smaller. The quiz tests understanding of image contents at each step and consequences of skipping steps. This pattern leverages Docker multi-stage builds for efficient images.