# Optimized Dockerfile for FastAPI-based AI service # Fixes all identified issues from container logs analysis ARG BASE_IMAGE=python:3.10-slim # Stage 1: builder (installs Python deps into a venv with build tooling) FROM ${BASE_IMAGE} AS builder ARG DEBIAN_FRONTEND=noninteractive ENV TZ=Etc/UTC # Install build tools only in builder RUN apt-get update && apt-get install -y --no-install-recommends \ tzdata \ build-essential \ python3 \ python3-pip \ python3-venv \ python3-dev \ tesseract-ocr \ poppler-utils \ ffmpeg \ && ln -fs /usr/share/zoneinfo/$TZ /etc/localtime \ && dpkg-reconfigure -f noninteractive tzdata \ && rm -rf /var/lib/apt/lists/* # Create virtual environment ENV VIRTUAL_ENV=/opt/venv RUN python3 -m venv "$VIRTUAL_ENV" ENV PATH="$VIRTUAL_ENV/bin:$PATH" WORKDIR /app COPY requirements.txt ./ # Upgrade pip and install dependencies with fixed versions RUN pip install --upgrade pip && \ pip install --prefer-binary -r requirements.txt # Stage 2: runtime (minimal runtime deps + venv from builder) FROM ${BASE_IMAGE} AS runtime ARG DEBIAN_FRONTEND=noninteractive ENV TZ=Etc/UTC # Install only runtime system packages; keep minimal RUN apt-get update && apt-get install -y --no-install-recommends \ tzdata \ tesseract-ocr \ poppler-utils \ ffmpeg \ && ln -fs /usr/share/zoneinfo/$TZ /etc/localtime \ && dpkg-reconfigure -f noninteractive tzdata \ && rm -rf /var/lib/apt/lists/* # Copy Python environment from builder COPY --from=builder /opt/venv /opt/venv ENV PATH="/opt/venv/bin:$PATH" # App WORKDIR /app COPY . . # Fixed environment variables for all identified issues ENV HF_HOME=/app/.cache/huggingface \ XDG_CACHE_HOME=/tmp \ TORCH_HOME=/tmp/torch \ WHISPER_CACHE=/tmp/whisper \ PYTHONUNBUFFERED=1 \ PYTHONPATH=/app \ GGUF_N_THREADS=4 \ GGUF_N_BATCH=64 \ OMP_NUM_THREADS=4 \ MKL_NUM_THREADS=4 \ NUMEXPR_NUM_THREADS=4 \ OPENVINO_TELEMETRY_DIR=/tmp/openvino_telemetry \ MPLCONFIGDIR=/tmp/matplotlib \ PRELOAD_GGUF=false # Create writable directories with proper permissions RUN mkdir -p /app/.cache/huggingface /tmp/uploads /tmp/huggingface /tmp/torch /tmp/whisper /tmp/openvino_telemetry /tmp/matplotlib && \ chmod -R 777 /app/.cache /tmp # Create optimized entrypoint script RUN echo '#!/bin/bash\n\ echo "[ENTRYPOINT] Starting optimized container startup..."\n\ echo "[ENTRYPOINT] Cleaning specific caches only..."\n\ rm -rf /tmp/huggingface/* /tmp/torch/* /tmp/whisper/* || true\n\ echo "[ENTRYPOINT] Preparing writable directories..."\n\ mkdir -p /tmp/uploads /tmp/huggingface /tmp/torch /tmp/whisper /tmp/openvino_telemetry /tmp/matplotlib\n\ chmod -R 777 /tmp\n\ echo "[ENTRYPOINT] Setting up environment..."\n\ export OMP_NUM_THREADS=4\n\ export OPENVINO_TELEMETRY_DIR=/tmp/openvino_telemetry\n\ export MPLCONFIGDIR=/tmp/matplotlib\n\ echo "[ENTRYPOINT] Starting application..."\n\ exec "$@"' > /entrypoint.sh && chmod +x /entrypoint.sh ENTRYPOINT ["/entrypoint.sh"] EXPOSE 7860 # Use uvicorn with no-reload to prevent duplicate route registration CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860", "--no-reload"]