# Use lighter Python image optimized for production FROM python:3.10-slim LABEL maintainer="Yuwarath Suktrakool " # Set environment variables for optimization and configuration paths ENV PYTHONUNBUFFERED=1 \ PYTHONDONTWRITEBYTECODE=1 \ PIP_NO_CACHE_DIR=1 \ PIP_DISABLE_PIP_VERSION_CHECK=1 \ OMP_NUM_THREADS=1 \ MKL_NUM_THREADS=1 \ TORCH_NUM_THREADS=1 \ MPLCONFIGDIR=/home/appuser/.config/matplotlib \ YOLO_CONFIG_DIR=/home/appuser/.config/Ultralytics # Create non-root user RUN useradd -m appuser && \ mkdir -p /app && \ chown appuser:appuser /app # Set the working directory WORKDIR /app # Create and set permissions for config directories RUN mkdir -p /home/appuser/.config/matplotlib && \ mkdir -p /home/appuser/.config/Ultralytics && \ mkdir -p /home/appuser/.local && \ chown -R appuser:appuser /home/appuser # Install system dependencies RUN apt-get update && \ apt-get install -y --no-install-recommends \ ffmpeg libsm6 libxext6 git \ && rm -rf /var/lib/apt/lists/* # Clone YOLOv5 and remove .git to reduce size RUN git clone --depth 1 https://github.com/ultralytics/yolov5.git && \ rm -rf yolov5/.git # Copy requirements first to leverage Docker cache COPY requirements.txt . # Install Python dependencies RUN pip install --no-cache-dir -r requirements.txt && \ chown -R appuser:appuser /app # Copy application code COPY . . RUN chown -R appuser:appuser /app # Switch to non-root user USER appuser # Run with workers optimized for CPU CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860", "--workers", "1"]