0Pricing
MLOps Academy · Lezione

Alleggerire le immagini con build multi-stage

Riduca le dimensioni dell'immagine separando i layer di build da quelli di runtime.

Alleggerire le immagini con build multi-stage è una lezione MLOps Academy gratuita su CoddyKit. Questa è la lezione 2 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento MLOps Academy, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso MLOps Academy include 4 lezioni in totale.

Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.

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 builder

Install 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.txt

Start 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-slim

Copy 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/.local

Bring 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 . /app

Fix 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:$PATH

Only 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-api

Quick 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. ✅

Domande Frequenti

La lezione «Alleggerire le immagini con build multi-stage» è gratuita?

Sì — il testo completo di «Alleggerire le immagini con build multi-stage» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso MLOps Academy, passa a CoddyKit PRO. Il corso MLOps Academy include 4 lezioni in totale.

Cosa imparerò in «Alleggerire le immagini con build multi-stage»?

Riduca le dimensioni dell'immagine separando i layer di build da quelli di runtime. Eserciti MLOps Academy con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.

Ho bisogno di esperienza per iniziare MLOps Academy?

Non è richiesta alcuna esperienza precedente. MLOps Academy su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 2 di 4.

Quanto tempo richiede la lezione «Alleggerire le immagini con build multi-stage»?

La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.

Posso scrivere ed eseguire codice in questa lezione MLOps Academy?

Sì. Ogni lezione MLOps Academy include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.

Tutte le lezioni di questo corso

  1. Scrivere un Dockerfile per una model API
  2. Alleggerire le immagini con build multi-stage
  3. Passare la configurazione tramite variabili d'ambiente
  4. Eseguire e testare il container in locale
← Torna a MLOps Academy