0Pricing
MLOps Academy · Lezione

Scrivere un Dockerfile per una model API

Containerizzi il servizio FastAPI passo dopo passo.

Scrivere un Dockerfile per una model API è una lezione MLOps Academy gratuita su CoddyKit. Questa è la lezione 1 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.

Why a Dockerfile

Your model API runs on your laptop but breaks elsewhere. A Dockerfile is a recipe that builds the exact same environment everywhere.

Pick a Base Image

Every Dockerfile starts FROM a base image. A slim Python image gives you the interpreter without a bloated operating system.

FROM python:3.11-slim

Set a Working Directory

The WORKDIR instruction picks the folder your commands run in. It is created if missing, so later paths stay clean and predictable.

WORKDIR /app

Copy Requirements First

Copy requirements.txt alone before your code. Docker caches this layer, so installs are skipped when only your app changes.

COPY requirements.txt .

Install Dependencies

Run pip install inside the image. The --no-cache-dir flag skips the pip cache and keeps the final image a little smaller.

RUN pip install --no-cache-dir -r requirements.txt

Copy Your Code

Now COPY the rest of your project in. Doing this after the install means code edits do not bust the dependency cache layer.

COPY . .

Document the Port

The EXPOSE instruction declares which port your API listens on. It is documentation for humans and tools, not an actual open.

EXPOSE 8000

Define the Start Command

The CMD sets what runs when the container starts. Here it launches Uvicorn to serve your FastAPI model app.

CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "8000"]

Bind to All Interfaces

Inside a container you must serve on 0.0.0.0, not 127.0.0.1. Otherwise the host cannot reach your model through the mapped port.

Build the Image

Turn the recipe into an image with docker build. The -t flag tags it with a name you can run and push later.

docker build -t model-api .

Layers Are Cached

Each instruction becomes a cached layer. Order them from least to most frequently changed so rebuilds stay fast.

Quick Check

Let us check the most cache-friendly build order.

Recap

You wrote a Dockerfile: base image, workdir, cached install, copy, expose, and a start command. Your model API now builds the same anywhere. 🐳

Domande Frequenti

La lezione «Scrivere un Dockerfile per una model API» è gratuita?

Sì — il testo completo di «Scrivere un Dockerfile per una model API» è 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 «Scrivere un Dockerfile per una model API»?

Containerizzi il servizio FastAPI passo dopo passo. 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 1 di 4.

Quanto tempo richiede la lezione «Scrivere un Dockerfile per una model API»?

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