By Sagar Shankaran, Founder of CallSphere
Learn how to build production-ready Docker images for AI agents using multi-stage builds, layer caching, slim base images, and security scanning to create fast, secure containers.
Key takeaways
AI agent images tend to bloat quickly. Python alone adds hundreds of megabytes. Add PyTorch, transformers, or LangChain and you can easily reach 5-10 GB. Large images mean slow deployments, slow autoscaling, wasted storage, and increased attack surface. Multi-stage builds solve this by separating the build environment from the runtime environment.
Most tutorials start with something like this:
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
WORKDIR /app
COPY . .
RUN pip install -r requirements.txt
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]
This image includes the full Python distribution, pip cache, build tools, header files, and every intermediate layer. A typical AI agent built this way produces a 3+ GB image.
Separate dependency installation from the final runtime image:
# Stage 1: Build dependencies
FROM python:3.12-slim AS builder
WORKDIR /build
RUN apt-get update && apt-get install -y --no-install-recommends \
gcc \
python3-dev \
&& rm -rf /var/lib/apt/lists/*
COPY requirements.txt .
RUN pip install --no-cache-dir --prefix=/install -r requirements.txt
# Stage 2: Runtime
FROM python:3.12-slim AS runtime
WORKDIR /app
# Copy only installed packages from builder
COPY --from=builder /install /usr/local
# Copy application code
COPY src/ ./src/
COPY main.py .
# Non-root user for security
RUN useradd --create-home agent
USER agent
EXPOSE 8000
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]
The runtime stage contains no compiler, no pip cache, and no build artifacts. This typically cuts image size by 40-60%.
Hear it before you finish reading
Talk to a live CallSphere AI voice agent for IT support in your browser — 60 seconds, no signup.
Docker caches layers based on instruction order. Place infrequently changing layers first:
FROM python:3.12-slim AS runtime
WORKDIR /app
# Layer 1: System dependencies (rarely changes)
RUN apt-get update && apt-get install -y --no-install-recommends \
libpq5 \
&& rm -rf /var/lib/apt/lists/*
# Layer 2: Python dependencies (changes weekly)
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
# Layer 3: Application code (changes on every commit)
COPY src/ ./src/
COPY main.py .
When only your application code changes, Docker reuses cached layers for system packages and Python dependencies — rebuilds take seconds instead of minutes.
Split your requirements to maximize cache hits:
# requirements-base.txt (stable dependencies)
fastapi==0.115.0
uvicorn==0.34.0
pydantic==2.10.0
httpx==0.28.0
# requirements-ai.txt (AI-specific, changes more often)
openai==1.65.0
langchain-core==0.3.30
tiktoken==0.8.0
# requirements.txt (combines both)
-r requirements-base.txt
-r requirements-ai.txt
Scan your images before pushing to a registry:
# Scan with Trivy
trivy image myregistry/ai-agent:1.0.0
# Scan with Docker Scout
docker scout cves myregistry/ai-agent:1.0.0
Integrate scanning into your CI pipeline so vulnerabilities are caught before deployment.
Prevent large files from entering the build context:
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.
# .dockerignore
__pycache__/
*.pyc
.git/
.env
*.onnx
*.bin
models/
data/
tests/
notebooks/
.venv/
Model weight files belong in a persistent volume or object storage, not baked into the container image.
A production-grade agent Dockerfile combining all practices:
FROM python:3.12-slim AS builder
WORKDIR /build
COPY requirements.txt .
RUN pip install --no-cache-dir --prefix=/install -r requirements.txt
FROM python:3.12-slim
WORKDIR /app
COPY --from=builder /install /usr/local
COPY src/ ./src/
COPY main.py .
RUN useradd --create-home agent
USER agent
EXPOSE 8000
HEALTHCHECK --interval=30s --timeout=5s \
CMD python -c "import httpx; httpx.get('http://localhost:8000/health').raise_for_status()"
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]
Alpine uses musl libc instead of glibc, which causes compatibility issues with many Python scientific packages including NumPy, pandas, and PyTorch. Stick with python:3.12-slim (Debian-based) for AI workloads. The size difference is minimal after a multi-stage build, and you avoid hours of debugging C extension compilation failures.
Never bake model weights into your Docker image. Instead, store them in object storage like S3 or a Kubernetes Persistent Volume. Have your agent download or mount models at startup. This keeps images small and lets you update models independently of code deployments.
A well-optimized AI agent image without local model weights should be between 200 MB and 800 MB depending on dependencies. If your image exceeds 1 GB without model files, investigate which packages are driving the size using docker history and consider removing unused dependencies.
#Docker #AIDeployment #ContainerOptimization #DevOps #Security #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.
See how Circini's automated incident management pipeline turns alert emails into triaged Jira tickets using Snowflake Cortex AI, GPT-4.1, Airflow & MS Teams.
How to build a safety eval pipeline that runs known jailbreak corpora, prompt-injection attacks, and tool-misuse scenarios on every release — and gates merges on it.
Inside NVIDIA OpenShell — the open-source secure runtime for autonomous desktop agents. Sandboxing, policy enforcement, and why it matters in 2026.
Stop the agent BEFORE it does the wrong thing. How to wire input and output guardrails in the OpenAI Agents SDK with cheap classifiers and an eval suite that proves they work.
NeMo Guardrails and LlamaGuard solve overlapping problems with different architectures. The trade-offs once you push them past 100 RPS in production agent stacks.
Prompt injection is still the top open agent security risk in 2026. The five defense patterns that work, and the two that do not — with real attack-and-defend examples.
© 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