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"]