---
title: "Containerize It: A Guide to Deploying Your AI App with Docker and FastAPI"
date: "2026-03-21T09:31:11+07:00"
description: "Learn how to package your AI applications into portable, scalable containers using Docker and the high-performance FastAPI framework."
author: "AI Assistant"
image: "/images/post/containerize-ai-app-docker-fastapi.jpg"
alt: "Docker and FastAPI integration"
tags: ["docker", "fastapi", "deployment", "mlops", "containerization"]
---

Building an AI model is only half the battle. Bringing it into production and making it accessible through a robust API is a critical next step. For developers in 2026, the combination of **FastAPI** for building high-performance APIs and **Docker** for containerization has become the gold standard for deploying AI applications.

In this tutorial, we'll walk through the process of containerizing a simple AI application—a sentiment analysis tool.

## Why Containerize AI Apps?

- **Dependency Management:** AI apps often have complex dependencies (e.g., PyTorch, TensorFlow). Docker ensures that your app runs in an identical environment, regardless of where it's deployed.
- **Reproducibility:** Eliminate the "it works on my machine" problem.
- **Portability:** Easily move your application between local development, staging, and any cloud provider.
- **Scalability:** Containers can be easily scaled up or down using orchestrators like Kubernetes.

## Step 1: Build a Simple FastAPI App

Create a file named `main.py` with the following code:

```python
from fastapi import FastAPI
from pydantic import BaseModel
from transformers import pipeline

app = FastAPI(title="Sentiment Analysis API")

# Initialize the sentiment analysis pipeline
# In production, you'd want to pre-download the model
classifier = pipeline("sentiment-analysis")

class TextData(BaseModel):
    text: str

@app.post("/predict")
def predict_sentiment(data: TextData):
    result = classifier(data.text)
    return {"prediction": result}

@app.get("/")
def read_root():
    return {"message": "Welcome to the Sentiment Analysis API!"}
```

## Step 2: Create a `requirements.txt`

List your application's dependencies:

```text
fastapi
uvicorn
transformers
torch
pydantic
```

## Step 3: Write the `Dockerfile`

Now, we'll create the instructions for Docker to build our image:

```dockerfile
# Use an official Python runtime as a parent image
FROM python:3.10-slim

# Set the working directory in the container
WORKDIR /app

# Install system dependencies
RUN apt-get update && apt-get install -y \
    build-essential \
    && rm -rf /var/lib/apt/lists/*

# Copy the requirements file into the container
COPY requirements.txt .

# Install Python dependencies
RUN pip install --no-cache-dir -r requirements.txt

# Copy the application code into the container
COPY main.py .

# Expose the port the app runs on
EXPOSE 8000

# Command to run the application
CMD ["uvicorn", "main.py:app", "--host", "0.0.0.0", "--port", "8000"]
```

## Step 4: Build and Run

With your files ready, open your terminal and run:

```bash
# Build the Docker image
docker build -t sentiment-api .

# Run the container
docker run -p 8000:8000 sentiment-api
```

Your API is now live at `http://localhost:8000`!

## Best Practices for AI Containers

1.  **Pre-download Models:** Don't download model weights when the container starts. Instead, include them in the image during the build process or mount them as a volume.
2.  **Use Multi-Stage Builds:** Keep your production image small by using multi-stage builds to separate the build environment from the runtime environment.
3.  **Optimize for Caching:** Order your `Dockerfile` instructions from least frequent to most frequent changes (e.g., install dependencies before copying source code) to leverage Docker's layer caching.
4.  **Security:** Run your application as a non-root user for better security.

## Conclusion

Containerizing your AI app with Docker and FastAPI is a powerful way to ensure your model is production-ready. By following these steps, you've created a portable, scalable, and reproducible environment for your application to thrive in any environment.

**What's Next?** Try deploying your container to a cloud provider like Google Cloud Run or AWS ECS!
