Slim Images with Multi-Stage Builds
Cut image size by separating build and runtime layers.
Slim Images with Multi-Stage Builds is a free MLOps Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the MLOps Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Big Images Hurt
A bloated image is slow to push, pull, and start, and it carries more to patch. Multi-stage builds ship only what you actually need.
The Core Idea
You use one stage to build and a second clean stage to run. Build tools stay behind, so the final image stays small.
Name a Build Stage
Add AS builder to a FROM line to name a stage. You can reference that name later to pull files out of it.
FROM python:3.11 AS builderInstall Into the Builder
In the builder, install dependencies to a known prefix. The --user flag drops everything neatly under one copyable folder.
RUN pip install --user --no-cache-dir -r requirements.txtStart the Runtime Stage
Open a fresh, slim stage for what actually runs. It has none of the compilers or caches the builder needed.
FROM python:3.11-slimCopy Only the Artifacts
Use COPY --from=builder to lift just the installed packages into the runtime stage. The heavy build layer is left behind.
COPY --from=builder /root/.local /root/.localBring in Your Code
Copy your application source into the slim stage too. Now the final image holds your code plus only its runtime dependencies.
COPY . /appFix the Path
User-installed packages live under .local, so add that to PATH. Otherwise the runtime cannot find your installed commands.
ENV PATH=/root/.local/bin:$PATHOnly Last Stage Ships
Docker keeps only the final stage in your image. Everything in earlier stages is discarded unless you explicitly copy it.
Try Distroless
For the smallest, safest result, run on a distroless base with no shell or package manager. Less surface means fewer things to exploit.
Measure the Win
Check the size drop with docker images. Slimming from over a gigabyte to a couple hundred megabytes is common and worth it.
docker images model-apiQuick Check
Let us check what a multi-stage build keeps.
Recap
You split build from runtime, copied just the artifacts with COPY --from, fixed PATH, and shrank the image. Lean and fast. ✅
Frequently asked questions
Is the “Slim Images with Multi-Stage Builds” lesson free?
Yes — the full text of “Slim Images with Multi-Stage Builds” is free to read here on the web, and the MLOps Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the MLOps Academy course, upgrade to CoddyKit PRO.
What will I learn in “Slim Images with Multi-Stage Builds”?
Cut image size by separating build and runtime layers. You practise MLOps Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start MLOps Academy?
No prior experience is required. MLOps Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Slim Images with Multi-Stage Builds” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this MLOps Academy lesson?
Yes. Every MLOps Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Write a Dockerfile for a Model API
- Slim Images with Multi-Stage Builds
- Pass Config via Environment Variables
- Run and Test the Container Locally