Production Engineering

From Notebook to the World: Deploying Your First AI App

It works on your machine... but that is not enough. Learn how to package your AI model with Docker and ship it to the cloud using industry-standard practices.

The Deployment Gap

Data Scientists often stop at the Jupyter Notebook. But a notebook is not a product. To turn your model into an app that users can actually access, you need Deployment.

This guide focuses on the modern path for AI deployment: Docker Containers running on Serverless Cloud.

1. The "It Works on My Machine" Problem

Imagine you send your code to a friend. They try to run it, but it crashes because they have a different version of Python, or they are missing a library.

Docker solves this. It lets you package your code, your python version, and all your libraries into a single "box" (called a Container). If the box works on your laptop, it is guaranteed to work on the cloud.

2. Step-by-Step: Deploying a Sentiment Analyzer

We will build a simple API that takes text and returns if it is Positive or Negative. We'll use FastAPI (the industry standard for AI APIs) and TextBlob (a simple NLP library).

Step A: The Application Code

Create a file named main.py:

main.pypython
from fastapi import FastAPI
from pydantic import BaseModel
from textblob import TextBlob
import os

app = FastAPI()

# 1. Define the input format (Data Validation)
class TextInput(BaseModel):
    text: str

# 2. Define the prediction endpoint
@app.post("/predict")
def predict_sentiment(input_data: TextInput):
    # Run the model
    analysis = TextBlob(input_data.text)
    polarity = analysis.sentiment.polarity
    
    # Determine label
    sentiment = "neutral"
    if polarity > 0:
        sentiment = "positive"
    elif polarity < 0:
        sentiment = "negative"
        
    return {
        "text": input_data.text,
        "sentiment": sentiment,
        "score": round(polarity, 2),
        "version": "v1.0"
    }

@app.get("/health")
def health_check():
    return {"status": "ok"}
Note: A robust API using FastAPI and Pydantic for data validation.

Step B: The Requirements

Create a file named requirements.txt:

requirements.txttext
fastapi
uvicorn
textblob
pydantic

Step C: The Dockerfile

This is the recipe for your container. It tells Docker how to build your app. Create a file named Dockerfile (no extension):

Dockerfiledockerfile
# 1. Start with a lightweight Python base
FROM python:3.9-slim

# 2. Set working directory
WORKDIR /app

# 3. Copy requirements FIRST (for Docker caching speed)
COPY requirements.txt .

# 4. Install dependencies
RUN pip install --no-cache-dir -r requirements.txt

# 5. Copy the rest of the app code
COPY . .

# 6. Command to run the app
# host 0.0.0.0 is critical for Docker networking
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8080"]
Note: A production-ready Docker setup using a lightweight Python image.

Step D: Run It

Open your terminal and run these commands to build and test your container locally.

Terminalbash
# 1. Build the image
docker build -t sentiment-app .

# 2. Run the container
# We map port 8080 on your machine to 8080 in the container
docker run -p 8080:8080 sentiment-app

3. Going to the Cloud (Serverless)

You don't need to manage complex servers (EC2) anymore. Modern AI apps use Serverless Containers. You just give the cloud provider your Docker image, and they handle the scaling.

ProviderService NameWhy use it?
Google CloudCloud RunEasiest for beginners. Scales to zero (costs $0 if no one uses it).
AWSApp RunnerSimpler than the complex ECS/EKS options.
AzureContainer AppsGreat if you are already in the Microsoft ecosystem.

Important: Secrets Management

Never put API keys (like OPENAI_API_KEY) in your code or Dockerfile. Use Environment Variables in your cloud dashboard. In Python, access them via os.getenv("KEY_NAME").

4. Automating with CI/CD

CI/CD (Continuous Integration / Deployment) means: "When I push code to GitHub, automatically update the live app."

The Manual Way

  1. Edit code locally
  2. Build Docker image manually
  3. Push image to registry
  4. Log into Cloud Console
  5. Click "Deploy Revision"

The Automated Way

  1. git push origin main
  2. GitHub Actions detects change
  3. It runs tests automatically
  4. It tells Cloud Run to update
  5. Deployment Complete

Your Deployment Checklist

  • Wrap your app in Docker.
  • Test it locally using docker run.
  • Push it to a registry (like Docker Hub or GCR).
  • Deploy it to a Serverless platform (Cloud Run).
  • Set up GitHub Actions to automate it.

Once you master this pipeline, you are no longer just a model trainer; you are an AI Engineer capable of shipping real software.