By Sagar Shankaran, Founder of CallSphere
Build production-ready Docker images for AI agents using multi-stage builds, proper dependency management, non-root users, and environment variable configuration for reproducible deployments.
Key takeaways
An AI agent that works on your laptop but fails in staging is a liability, not an asset. Docker containers eliminate the "works on my machine" problem by packaging your agent code, Python runtime, system libraries, and dependencies into a single portable image. Every environment — development, CI, staging, production — runs the exact same artifact.
For AI agents specifically, containerization solves three additional problems: pinning exact versions of ML libraries that have breaking changes between minor releases, isolating GPU drivers and CUDA dependencies, and enabling horizontal scaling through orchestrators like Kubernetes.
Start with the simplest possible Dockerfile for a FastAPI-based agent service:
flowchart LR
INPUT(["User intent"])
PARSE["Parse plus<br/>classify"]
PLAN["Plan and tool<br/>selection"]
AGENT["Agent loop<br/>LLM plus tools"]
GUARD{"Guardrails<br/>and policy"}
EXEC["Execute and<br/>verify result"]
OBS[("Trace and metrics")]
OUT(["Outcome plus<br/>next action"])
INPUT --> PARSE --> PLAN --> AGENT --> GUARD
GUARD -->|Pass| EXEC --> OUT
GUARD -->|Fail| AGENT
AGENT --> OBS
style AGENT fill:#4f46e5,stroke:#4338ca,color:#fff
style GUARD fill:#f59e0b,stroke:#d97706,color:#1f2937
style OBS fill:#ede9fe,stroke:#7c3aed,color:#1e1b4b
style OUT fill:#059669,stroke:#047857,color:#fff
FROM python:3.12-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
EXPOSE 8000
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000"]
This works but has several production problems: it runs as root, includes build tools in the final image, and does not layer dependencies efficiently.
A multi-stage build separates dependency installation from the runtime image, cutting the final image size dramatically:
Hear it before you finish reading
Talk to a live CallSphere AI voice agent for IT support in your browser — 60 seconds, no signup.
# Stage 1: Build dependencies
FROM python:3.12-slim AS builder
WORKDIR /build
COPY requirements.txt .
RUN pip install --no-cache-dir --prefix=/install -r requirements.txt
# Stage 2: Runtime image
FROM python:3.12-slim AS runtime
RUN groupadd -r agent && useradd -r -g agent agent
WORKDIR /app
COPY --from=builder /install /usr/local
COPY --chown=agent:agent . .
USER agent
EXPOSE 8000
HEALTHCHECK --interval=30s --timeout=5s --retries=3 \
CMD python -c "import urllib.request; urllib.request.urlopen('http://localhost:8000/health')"
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000"]
This approach yields images around 250 MB instead of 800+ MB, runs as a non-root user, and includes a built-in health check.
Pin every dependency to exact versions for reproducibility:
# requirements.txt
fastapi==0.115.6
uvicorn[standard]==0.34.0
openai-agents==0.0.7
pydantic==2.10.4
python-dotenv==1.0.1
httpx==0.28.1
Generate pinned versions from your working environment:
pip freeze > requirements.txt
For complex projects, use a two-file strategy: requirements.in with your direct dependencies and pip-compile to generate the locked requirements.txt.
Never bake secrets into your Docker image. Pass them at runtime:
# In Dockerfile — set non-secret defaults only
ENV AGENT_MODEL=gpt-4o
ENV AGENT_TIMEOUT=30
ENV LOG_LEVEL=info
Then load them in your application:
Still reading? Stop comparing — try CallSphere live.
See the IT support AI agent handle a real call — complete, industry-specific, and live in your browser. No signup.
# app/config.py
from pydantic_settings import BaseSettings
class Settings(BaseSettings):
openai_api_key: str # Required — no default, fails fast if missing
agent_model: str = "gpt-4o"
agent_timeout: int = 30
log_level: str = "info"
class Config:
env_file = ".env"
settings = Settings()
Run the container passing secrets at runtime:
docker run -e OPENAI_API_KEY=sk-proj-xxx -p 8000:8000 agent-service:latest
Prevent large and sensitive files from being copied into the image:
# .dockerignore
.git
.env
__pycache__
*.pyc
.venv
tests/
docs/
*.md
.mypy_cache
# Build the image
docker build -t agent-service:1.0.0 .
# Run with environment variables
docker run -d \
--name agent \
-e OPENAI_API_KEY=sk-proj-xxx \
-p 8000:8000 \
agent-service:1.0.0
# Verify it is healthy
docker ps
curl http://localhost:8000/health
Add dependent services like Redis for session storage:
# docker-compose.yml
services:
agent:
build: .
ports:
- "8000:8000"
environment:
- OPENAI_API_KEY=${OPENAI_API_KEY}
- REDIS_URL=redis://redis:6379/0
depends_on:
- redis
redis:
image: redis:7-alpine
ports:
- "6379:6379"
Use multi-stage builds so build tools and compilation artifacts stay out of the final image. Start from python:3.12-slim instead of the full image. Add a .dockerignore to exclude tests, documentation, and version control files. If you need PyTorch or other large ML libraries, look for CPU-only variants when GPU is not required.
No. Embedding model weights creates multi-gigabyte images that are slow to push, pull, and deploy. Instead, download weights at startup from a model registry or object storage, or mount them as a volume. This also lets you update models without rebuilding the entire container image.
Use docker exec -it agent /bin/bash to open a shell inside the running container. Check logs with docker logs agent --tail 100. For FastAPI specifically, set LOG_LEVEL=debug as an environment variable to get detailed request logging without rebuilding the image.
#Docker #AIAgents #Containerization #DevOps #Python #AgenticAI #LearnAI #AIEngineering

Written by
Sagar Shankaran· Founder, CallSphere
LinkedInSagar Shankaran is the founder of CallSphere, where he builds production AI voice and chat agents deployed across healthcare, hospitality, real estate, and home services. He writes about agentic AI, LLM engineering, and shipping voice agents that handle real calls in production.
See how AI voice agents work for your industry. Live demo available -- no signup required.
How we built a fault-tolerant HVAC emergency triage and tech-dispatch platform on Kubernetes — three-tier CQRS, 11 micro-agents on the OpenAI Agents SDK + LangGraph, NATS JetStream, DTMF/SMS/WebSocket acceptance, circuit breakers, and an evaluation pipeline that catches regressions before they wake a tech at 3 AM.
Head-to-head: OpenAI Frontier and Anthropic's managed agent stack — strengths, fit, and what each means for enterprise AI voice and chat deployment.
Meta is building Hatch, a consumer AI agent that operates DoorDash, Reddit, and other third-party apps — Meta's answer to OpenClaw and Google Remy.
OpenAI Frontier — the new enterprise platform announced this week for building, deploying, and managing AI agents that do real work.
Q1 2026 saw a record acquisition wave: Aircall bought Vogent (May), Meta acquired Manus and PlayAI, OpenAI closed six deals. The voice AI consolidation phase has begun.
Microsoft's Copilot for Sales shipped 2026 updates that knit Dynamics, Outlook, and Teams into a single agentic surface. Here's the playbook, the per-seat pricing.
© 2026 CallSphere Inc. All rights reserved.
Made within San Francisco
Watch how CallSphere handles real customer calls, schedules appointments, and processes payments — live.
Try Live DemoBook a DemoCalculate Your ROI