Images mit Multi-Stage-Builds verkleinern
Verkleinern Sie das Image, indem Sie Build- und Laufzeitschichten trennen.
Images mit Multi-Stage-Builds verkleinern ist eine kostenlose MLOps Academy-Lektion auf CoddyKit. Dies ist Lektion 2 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des MLOps Academy-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der MLOps Academy-Kurs umfasst insgesamt 4 Lektionen.
Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.
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. ✅
Häufig gestellte Fragen
Ist die Lektion „Images mit Multi-Stage-Builds verkleinern“ kostenlos?
Ja — der vollständige Text von „Images mit Multi-Stage-Builds verkleinern“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des MLOps Academy-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der MLOps Academy-Kurs umfasst insgesamt 4 Lektionen.
Was lerne ich in „Images mit Multi-Stage-Builds verkleinern“?
Verkleinern Sie das Image, indem Sie Build- und Laufzeitschichten trennen. Du übst MLOps Academy mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.
Brauche ich Erfahrung, um MLOps Academy zu starten?
Keine Vorkenntnisse erforderlich. MLOps Academy auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 2 von 4.
Wie lange dauert die Lektion „Images mit Multi-Stage-Builds verkleinern“?
Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.
Kann ich in dieser MLOps Academy-Lektion Code schreiben und ausführen?
Ja. Jede MLOps Academy-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.
Alle Lektionen in diesem Kurs
- Eine Dockerfile für eine Model-API schreiben
- Images mit Multi-Stage-Builds verkleinern
- Konfiguration über Umgebungsvariablen übergeben
- Den Container lokal ausführen und testen