Module 2: Local & Container Deployment

1. Introduction: Local & Container Deployment

Overview

This is the first capsule of Module 2. Here you'll understand why local deployment with Docker Compose multi-container is the base of everything that comes in this guide — and why "local" doesn't mean "toy." A well-configured local environment replicates production faithfully: same service structure, same networking configuration, same health checks. The difference from production is where it runs, not how it's configured.

Why it matters: You already made an informed decision in Module 1 with your decision matrix. Regardless of which strategy you chose, mastering local deployment with Docker Compose is fundamental. It's your development environment, your staging, and for many cases it's legitimate production. Modules 4 (LocalStack), 6 (migration), and 8 (capstone) build on the Compose you configure here.


Where Are We in the Guide?

Context

Phase 1: Deployment Strategies (Modules 1-3)
├── Module 1: Understanding Deployment Options    ✅ COMPLETED
├── Module 2: Local & Container Deployment        ← YOU ARE HERE
└── Module 3: Serverless & Lambda for AI

Phase 2: Cloud Infrastructure & Migration (Modules 4-6)
Phase 3: Alternatives & Production (Modules 7-8)

Transition from Module 1

In Module 1 you built a framework to decide. Here you start to implement. The first strategy you master is local deployment because:

  1. It's the base of your daily development
  2. Docker Compose is portable to any platform
  3. Your local environment must replicate production
  4. LocalStack (M4) integrates as a service in your Compose

What Docker Compose Multi-Container Is for AI

From one container to a system

The Docker guide (#15) taught you to containerize your app: one Dockerfile, one container, one service. But a production AI app isn't a single container:

Simple AI app (1 container):
└── FastAPI + OpenAI SDK

Production AI app (3-5 containers):
├── FastAPI (API gateway)
├── Redis (response cache)
├── ChromaDB/Qdrant (vector store)
├── Worker (async processing)
└── Nginx (reverse proxy + SSL)

Docker Compose orchestrates all these containers as a system: it brings them up in the right order, connects their networks, shares volumes, and verifies that each one is healthy before accepting traffic.

The restaurant analogy

A single container is like a chef who cooks, serves, and charges. It works if you have 5 customers. Docker Compose is like an organized restaurant: chef in the kitchen, waiters serving, cashier charging, each with their role and communicating with each other. If the chef isn't ready, the waiters wait. If the register fails, the restaurant keeps cooking but doesn't charge.

Each category in code

So you see the concrete difference, this is what each pattern looks like:

# A single container (Docker guide #15)
docker run -p 8000:8000 my-ai-app

# Docker Compose multi-container (this module)
# docker-compose.yml
services:
  api:
    build: ./api
    ports:
      - "8000:8000"
    depends_on:
      cache:
        condition: service_healthy
  cache:
    image: redis:7-alpine
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
# One command brings up the whole system
docker compose up -d

# One command shuts it down
docker compose down

The key difference: with docker run you manage each container manually. With Compose, you declare your whole system in one file and Compose orchestrates it for you.


This Module's Technologies

ToolVersionWhat it does in this module
Docker Composev2.20+Orchestrates multi-container
FastAPI0.115+API gateway of your AI app
Redis7.xLLM response cache
Python3.10+The app's language
OpenAI SDK1.0+LLM calls
Pydantic2.0+Config and request validation
curl-Health checks and testing

Reference architecture

                    ┌──────────────────────────────────────────┐
                    │         Docker Compose Network           │
                    │                                          │
  User Request ──→  │  ┌─────────┐      ┌─────────┐           │
                    │  │ FastAPI  │ ←──→ │  Redis   │           │
                    │  │ :8000   │      │  :6379  │           │
                    │  │         │      │ (cache) │           │
                    │  │ /health │      └─────────┘           │
                    │  │ /ask    │                             │
                    │  └─────────┘                             │
                    │       │                                  │
                    │       ↓                                  │
                    │  OpenAI API (external)                   │
                    └──────────────────────────────────────────┘

Each service runs in its own container, with its own filesystem, its own internal network, and its own health checks. Docker Compose makes sure the ordering, communication, and persistence work without manual intervention.


Module Objective

By the end of this module you'll be able to:

  • ✅ Design a Docker Compose file for a multi-container AI app (FastAPI + Redis + AI service)
  • ✅ Implement environment configuration per environment (dev, staging, prod)
  • ✅ Configure volumes for persistent data and networking between services
  • ✅ Implement health checks that verify each service's real readiness
  • ✅ Define correct dependency ordering between services
  • ✅ Debug local deployment with docker compose logs, exec, and troubleshooting
  • ✅ Handle secrets and API keys safely without committing to Git

Professional objective

When you need to show your AI app to a colleague, a manager, or an investor, you'll run docker compose up and in 30 seconds you'll have a multi-service system running with health checks, cache, and production-like configuration. That's professional local deployment.


Module Roadmap

#CapsuleWhat you'll learnType
01Introduction (this one)Context, objectives, setupIntro
02Docker Compose for AI AppsCompose file, services, networks, volumesTechnical
03Environment ConfigurationVariables per environment, .env files, overridesTechnical
04Health Checks and DependenciesReadiness checks, dependency ordering, restart policiesTechnical
05Networking and CommunicationInternal networks, ports, service discoveryTechnical
06Debugging Local DeploymentLogs, exec, troubleshooting AI appsTechnical
07Secrets and Local SecurityAPI keys, .env, Docker secrets, gitignoreTechnical
08Project: Local Multi-Container AI AppComplete Compose with FastAPI + Redis + AIProject

Estimated duration: 1.25-1.5 hours.


Prerequisites

What you already know (from the Docker guide #15)

  • ✅ What a Dockerfile, image, and container are
  • ✅ How to build and run a container
  • ✅ Basic commands: docker build, docker run, docker ps
  • ✅ Dockerizing a FastAPI app

What you'll learn here (new)

  • Docker Compose multi-service (not just one container)
  • Orchestration: ordering, health checks, dependencies
  • Environment config per environment
  • Local deployment debugging
  • Local secrets management

Technical Setup

Verify tools

# Docker
docker --version
# Docker version 24.0+ expected

# Docker Compose (v2, integrated in Docker Desktop)
docker compose version
# Docker Compose version v2.20+ expected

# Python
python --version
# Python 3.10+ expected

Create the project structure

mkdir -p module-02/{api,worker}
cd module-02

# Initial structure
# module-02/
# ├── api/
# │   ├── main.py
# │   ├── requirements.txt
# │   └── Dockerfile
# ├── worker/
# │   ├── worker.py
# │   ├── requirements.txt
# │   └── Dockerfile
# ├── docker-compose.yml
# ├── docker-compose.override.yml
# ├── .env
# └── .env.example

OpenAI API key

You need an OpenAI API key for this module's examples. If you don't have one:

  1. Go to platform.openai.com
  2. Create an account (or use the existing one)
  3. Go to API Keys → Create new secret key
  4. Copy the key (starts with sk-proj-...)
# Create a .env file in your working directory
echo "OPENAI_API_KEY=sk-proj-your-key-here" > .env

# NEVER commit this file to Git
echo ".env" >> .gitignore

The examples use gpt-4o-mini, which is the cheapest model (~$0.15/1M input tokens). A full practice module shouldn't cost you more than $0.50.

Quick verification

# Verify that Docker works
docker run --rm hello-world

# Verify that Compose works
echo 'services:
  test:
    image: alpine
    command: echo "Compose works!"' > test-compose.yml

docker compose -f test-compose.yml up
# Expected output: "Compose works!"

rm test-compose.yml

Verify everything together

# Quick full verification script
echo "=== Docker ===" && docker --version \
  && echo "=== Compose ===" && docker compose version \
  && echo "=== Python ===" && python --version \
  && echo "=== OpenAI key ===" && ([ -f .env ] && echo ".env exists ✅" || echo ".env missing ❌") \
  && echo "=== All checks passed ==="

If everything shows correct versions and .env exists, you're ready for capsule 02.


Connection to the Project

This module's project: Local Multi-Container AI App

You'll build a complete Docker Compose with:

  • FastAPI as the API gateway (receives requests, processes them)
  • Redis as the response cache (avoids repeated LLM calls)
  • Worker (optional) for asynchronous processing
User Request
    ↓
┌─────────────────────┐
│ FastAPI (api)        │
│ Port 8000            │
│ ├── /health          │
│ ├── /ask             │──→ Check Redis cache
│ └── /ask (cache miss)│──→ Call OpenAI → Store in Redis
└─────────────────────┘
         ↕
┌─────────────────────┐
│ Redis (cache)        │
│ Port 6379 (internal) │
│ Response cache       │
└─────────────────────┘

This Compose extends in later modules:

  • M4: Add LocalStack as a service (local S3, Lambda)
  • M6: Use as a development environment for migration to AWS
  • M8: Base of the system deployed in production
Compose evolution throughout the guide:

M2: FastAPI + Redis (base)
 ↓
M4: FastAPI + Redis + LocalStack (local S3/Lambda)
 ↓
M6: Same Compose, code that works against LocalStack AND AWS
 ↓
M8: Local Compose → CI/CD → Production platform

The Compose you build in this module isn't a throwaway exercise. It's the guide's central artifact — the base on which you add layers of complexity module by module.

What is NOT a prerequisite (you learn it here)

Some concepts you might think you need to know but that this module teaches you:

ConceptDo you need it beforehand?Where you learn it
Docker Compose syntaxNoCapsule 02
.env files and overridesNoCapsule 03
Health checksNoCapsule 04
Docker networkingNoCapsule 05
Docker debuggingNoCapsule 06
Secrets managementNoCapsule 07

Limits: What This Module Does NOT Cover

  • Docker from scratch — Prerequisite: Docker Essentials Guide (#15)
  • Kubernetes — Enterprise-scale orchestration, out of scope
  • Cloud deployment — That comes in Modules 3-8
  • Docker Swarm — Compose is enough for this guide's scope
  • CI/CD setup — Prerequisite: CI/CD for AI Systems Guide (#16)

Signs of Success

By the end of this module:

  • docker compose up brings up your multi-container AI app in <30 seconds
  • ✅ The health checks verify that each service is operational
  • ✅ Redis caches responses and reduces LLM calls
  • ✅ You can switch between dev and staging configuration with a flag
  • ✅ You know how to diagnose when a service doesn't start (logs, exec, networking)
  • ✅ The API keys aren't in your code or in Git

Quick self-assessment

If you can answer these questions at the end of the module, you've mastered it:

  1. Why does an AI app need more than one container?
  2. What problem does depends_on with condition: service_healthy solve?
  3. What's the difference between ports and expose in Compose?
  4. How do you switch between dev and staging configuration without modifying the Compose file?
  5. What's the first thing you do when a service doesn't start?

If any seems hard now, don't worry — the answers become clear across the 8 capsules.


Summary

  • Docker Compose multi-container is the base of professional deployment for AI apps.
  • A well-configured local environment replicates production — it's not a toy.
  • A typical AI app has 3-5 services: API, cache, vector store, worker, reverse proxy.
  • This module covers the 7 key skills: Compose files, env config, health checks, networking, debugging, secrets, and capstone project.
  • This module builds the Compose that gets reused in M4 (LocalStack), M6 (migration), and M8 (production).
  • The final project is a multi-container AI app with a working FastAPI + Redis.
  • Prerequisite: Docker Essentials Guide (#15). If you know how to do docker build and docker run, you're ready.

Additional Resources

  1. Docker Compose Documentation — Complete official reference
  2. Docker Compose File Reference — Compose file specification
  3. FastAPI Docker Deployment — Official FastAPI guide with Docker
  4. Redis Docker Hub — Official Redis image
  5. Docker Networking Overview — Networking in Docker
  6. Docker Compose Best Practices — Compose in production