🐳 Lesson 9.1: Local Docker Deployment

 


🐳 Lesson 9.1: Local Docker Deployment


🎯 Learning Objectives

By the end of this lesson, learners will be able to:

  • Understand what Docker is and why it’s used

  • Containerize their AI agent application using Docker

  • Create a Dockerfile and docker-compose.yml for local testing

  • Run and test the AI agent in a Dockerized local environment


🔧 What Is Docker?

Docker is an open-source platform that allows you to package applications and their dependencies into containers. These containers are:

  • Lightweight

  • Portable

  • Isolated from your host system

  • Easy to deploy and replicate

In short: Docker lets you run your app anywhere, the same way, every time.


🧱 Why Use Docker for AI Agents?

Deploying an AI agent locally without Docker can result in issues like:

  • Dependency mismatches

  • Inconsistent environments across developers

  • Manual setup of Python environments and model files

With Docker, you get:

  • A repeatable build process

  • Version-controlled deployment

  • Easier testing and debugging

  • A path toward production deployment (cloud, server, etc.)


📁 Step-by-Step Guide to Local Docker Deployment


✅ Step 1: Project Structure Example

my-ai-agent/
├── app.py
├── requirements.txt
├── models/
│   └── model.bin (optional local LLM or embeddings)
├── Dockerfile
└── docker-compose.yml (optional)

📝 Step 2: Write a Dockerfile

Here’s a simple Dockerfile for an AI agent using Python and LangChain:

# Use an official Python base image
FROM python:3.11-slim

# Set working directory
WORKDIR /app

# Copy requirements and install them
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt

# Copy the application code
COPY . .

# Run the application
CMD ["python", "app.py"]

✅ If your agent runs on FastAPI or Flask, update the CMD accordingly:

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

📜 Step 3: Add requirements.txt

Include all your Python dependencies:

langchain
openai
streamlit
faiss-cpu
python-dotenv

If you use local models:

ollama
whisper

🧪 Step 4: Build and Run Your Docker Container

Run these commands from the terminal:

# Build the Docker image
docker build -t my-ai-agent .

# Run it
docker run -p 8000:8000 my-ai-agent

Now, your AI agent is live on http://localhost:8000 (or port of your choice).


⚙️ Optional: Use Docker Compose for Multi-Service Setup

If you’re also using services like PostgreSQL, Redis, or Ollama, use docker-compose.yml.

version: '3.8'
services:
  app:
    build: .
    ports:
      - "8000:8000"
    volumes:
      - .:/app
    depends_on:
      - ollama
  ollama:
    image: ollama/ollama
    ports:
      - "11434:11434"
    volumes:
      - ollama-data:/root/.ollama
volumes:
  ollama-data:

🧠 Debugging Common Issues

Problem Solution
Docker image builds slowly Use a .dockerignore file
Missing models Mount a volume or download at runtime
Ports not exposed Use -p or expose in docker-compose
File not found Ensure correct WORKDIR and COPY paths

🧪 Testing the Deployed Agent

Once deployed locally in Docker:

  • Test endpoints using Postman or Curl

  • Access the UI (Streamlit or Gradio) at the mapped port

  • Monitor logs with docker logs


📝 Assignment

Task: Containerize your AI agent using Docker and share a screenshot of it running at localhost.


🧩 Recap

Step Summary
1 Create a Dockerfile
2 Add all dependencies to requirements.txt
3 Build with docker build -t ...
4 Run with docker run -p ...
5 Test your app locally

🚀 What’s Next?

In the next lesson, you’ll learn how to take your Dockerized AI agent and deploy it on a remote server (e.g., VPS or cloud platform) for real-world use.


 

73