0Pricing
MLOps Academy · Lesson

Write a Dockerfile for a Model API

Containerize the FastAPI service step by step.

Write a Dockerfile for a Model API is a free MLOps Academy lesson on CoddyKit — lesson 1 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.

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. 🐳

Frequently asked questions

Is the “Write a Dockerfile for a Model API” lesson free?

Yes — the full text of “Write a Dockerfile for a Model API” 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 “Write a Dockerfile for a Model API”?

Containerize the FastAPI service step by step. 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 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Write a Dockerfile for a Model API” 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

  1. Write a Dockerfile for a Model API
  2. Slim Images with Multi-Stage Builds
  3. Pass Config via Environment Variables
  4. Run and Test the Container Locally
← Back to MLOps Academy