Lab 19: Deploying ML with FastAPI + Docker

Objective

Package and serve a trained ML model as a production REST API: FastAPI endpoint design, input validation with Pydantic, model serialisation, health checks, batch inference, authentication middleware, and containerisation.

Time: 45 minutes | Level: Practitioner | Docker Image: zchencow/innozverse-ai:latest


Background

Training a model is only half the work. Deployment involves:

Model → REST API → Authentication → Rate limiting → Monitoring → Container

Production concerns:
  - Latency:    p99 < 100ms for real-time threat scoring
  - Throughput: 1000+ requests/second for SIEM integration
  - Reliability: health checks, graceful degradation
  - Security:   API key auth, input validation, rate limiting
  - Observability: request logging, prediction drift tracking

Step 1: Train and Serialise the Model

📸 Verified Output:


Step 2: FastAPI Application

📸 Verified Output:


Step 3: Mock API Testing

📸 Verified Output:


Step 4–8: Capstone — Dockerfile + Deployment

📸 Verified Output:


Summary

Component
Tool
Purpose

API framework

FastAPI

Async, auto-docs, type-safe

Validation

Pydantic

Input sanitisation

Auth

API key header

Multi-tenant access control

Rate limiting

In-memory counter

Prevent abuse

Serialisation

pickle / joblib

Model persistence

Container

Docker

Reproducible deployment

Orchestration

docker-compose / k8s

Multi-replica scaling

Further Reading

Last updated