home
diamond Go Premium
Data Engineering Path  ·  Data & AI

Containerizing and Dockerizing AI Applications

+-------------------------------------------------------------------------------+
|                       MULTI-STAGE DOCKER BUILD FOR AI                         |
+-------------------------------------------------------------------------------+
|  Stage 1 (Builder): Heavy CUDA compiler, build tools, wheel downloads (~4GB)  |
|                                     |                                         |
|                                     v (copy only compiled wheels)             |
|  Stage 2 (Runtime): Slim Python base image, minimal dependencies (~800MB)     |
+-------------------------------------------------------------------------------+

Challenges in AI Containerization

  • Giant Image Sizes: PyTorch with CUDA support alone is over 2.5 GB.
  • GPU Driver Mismatches: Ensuring host NVIDIA driver is compatible with container CUDA runtime.
  • Layer Caching: Preventing costly pip install torch re-runs on every minor code tweak.

Production Multi-Stage Dockerfile

# Stage 1: Build & Dependencies
FROM python:3.11-slim as builder

WORKDIR /build

RUN apt-get update && apt-get install -y --no-install-recommends     build-essential     curl     && rm -rf /var/lib/apt/lists/*

COPY requirements.txt .
RUN pip install --no-cache-dir --user -r requirements.txt

# Stage 2: Minimal Runtime
FROM python:3.11-slim as runtime

WORKDIR /app

# Non-root security user
RUN useradd -m -u 1000 appuser

# Copy installed Python packages from builder
COPY --from=builder /root/.local /home/appuser/.local
ENV PATH=/home/appuser/.local/bin:$PATH

COPY . /app
RUN chown -R appuser:appuser /app

USER appuser

EXPOSE 8000

HEALTHCHECK --interval=30s --timeout=5s --start-period=5s --retries=3   CMD curl -f http://localhost:8000/health || exit 1

CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000", "--workers", "2"]
Find this content helpful? ☕ Buy me a coffee

Entity Details

Create New Item

help

Submit Technical Query

Have a question or run into an issue? Describe it below, upload an optional screenshot, and our engineering team will answer it!

image Attach image (optional)

Submit Feedback

build Free Developer Utility Free Tool
gavel

Privacy & Legal Disclaimer

1. Client-Side Browser Processing

All utility tools on DeepEngineerHub (including Image to PDF, Text Formatters, JSON Converters, and Encryptors) execute 100% locally within your client browser using WebAssembly and JavaScript. No uploaded images, text, or documents are transmitted, collected, or stored on remote servers.

2. Limitation of Liability ("As-Is" Provision)

Tools and services are provided free of charge for convenience and educational purposes "as-is" without warranties of any kind. DeepEngineerHub shall not be held liable for any data loss, formatting inconsistencies, or indirect damages resulting from tool usage.

3. Open Source & Third-Party Software

Certain utilities utilize open-source client libraries (such as jsPDF, Mermaid.js, Pyodide) licensed under MIT, Apache, or BSD open licenses. All intellectual property remains with their respective copyright holders.