0
0
MLOpsdevops~10 mins

Multi-stage builds for smaller images in MLOps - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Multi-stage builds for smaller images
Start: Define first stage
Build app in first stage
Define second stage
Copy only needed files from first stage
Build final smaller image
Use smaller image for deployment
Multi-stage builds use multiple steps in one Dockerfile to build and then copy only needed files, making the final image smaller.
Execution Sample
MLOps
FROM node:18 AS build
WORKDIR /app
COPY package.json .
RUN npm install
COPY . .
RUN npm run build

FROM node:18-slim
WORKDIR /app
COPY --from=build /app/dist ./dist
CMD ["node", "dist/index.js"]
This Dockerfile builds the app in the first stage, then copies only the built files to a smaller final image.
Process Table
StepActionStageFiles PresentImage Size Impact
1Start first stage with full node imagebuildAll source files copiedLarge size due to full dependencies
2Run npm install and buildbuildNode modules and build output createdSize grows due to dependencies and build artifacts
3Start second stage with slim node imagefinalEmpty except base image filesSmall base size
4Copy only /app/dist from build stagefinalOnly built files presentSize small, no dev dependencies
5Set command to run built appfinalReady to runFinal image optimized
6Image ready for deploymentfinalContains only runtime filesSmall image size achieved
💡 Final image contains only necessary runtime files, making it smaller than the build stage image.
Status Tracker
VariableStartAfter Step 2After Step 4Final
Files in build stagesource files onlysource + node_modules + build outputN/AN/A
Files in final stageemptyemptyonly /app/dist copiedonly /app/dist present
Image sizebase sizelarge due to dependenciesbase slim sizesmall optimized size
Key Moments - 3 Insights
Why do we use two FROM statements in one Dockerfile?
The first FROM builds the full app with all tools, the second FROM starts a clean smaller image to copy only needed files, as shown in execution_table steps 1 and 3.
What happens if we copy all files from the build stage instead of just the build output?
The final image will be large because it includes unnecessary source and node_modules files, losing the size benefit shown in step 4.
Why is the final image smaller even though it runs the same app?
Because it contains only the built app files without development dependencies, as seen in the variable_tracker for final stage files.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 4, what files are copied to the final image?
ANode modules only
BAll source files
C/app/dist only
DEmpty folder
💡 Hint
Check the 'Files Present' column at step 4 in the execution_table.
At which step does the image size become small and optimized?
AStep 6
BStep 2
CStep 5
DStep 4
💡 Hint
Look at the 'Image Size Impact' column and find when the final image is ready.
If we remove the second FROM line, how would the final image size change?
AIt would be smaller
BIt would be larger
CIt would be the same
DIt would fail to build
💡 Hint
Refer to the concept_flow and execution_table steps showing the purpose of the second stage.
Concept Snapshot
Multi-stage builds use multiple FROM statements in one Dockerfile.
First stage builds app with all dependencies.
Second stage copies only needed files to a smaller base image.
Result: smaller final image with just runtime files.
Use COPY --from=build to transfer files between stages.
Full Transcript
Multi-stage builds help create smaller Docker images by splitting the build process into stages. The first stage uses a full image to build the app with all dependencies. The second stage starts from a smaller base image and copies only the built app files from the first stage. This reduces the final image size by excluding development tools and source files. The Dockerfile uses multiple FROM statements to define stages and COPY --from to transfer files. This method is common in MLOps to optimize container sizes for deployment.