# 1. Base Image: Use a stable, slim Python image FROM python:3.10-slim # Set the environment variable for Python ENV PYTHONUNBUFFERED=1 # 2. System Dependencies: Install essential libraries for image processing and OCR RUN apt-get update && \ apt-get install -y --no-install-recommends \ libgl1 \ libgomp1 \ libsm6 libxext6 libxrender1 \ tesseract-ocr \ build-essential \ && apt-get clean && rm -rf /var/lib/apt/lists/* # 3. Application Setup WORKDIR /app # 4. Copy dependency file and install Python dependencies COPY requirements.txt . RUN pip install --no-cache-dir -r requirements.txt # 5. Copy ALL project files (This is where the .pkl and .h5 files arrive) # This step MUST be done before applying permissions to model files. COPY . . # 6. Final Permissions and Directory Fixes (Executed AFTER COPY) # ----------------------------------------------------------------- # Grant general access to /app so the non-root user can read/write RUN chmod -R 777 /app # Set up EASYOCR CACHE with permissions (Resolves PermissionError) ENV EASYOCR_MODULE_PATH /app/easyocr_models RUN mkdir -p /app/easyocr_models RUN chmod -R 777 /app/easyocr_models # CRITICAL FIX: Ensure individual model files are explicitly readable by the Gunicorn worker. # This prevents the "Model files missing" error during Gunicorn startup. RUN chmod 666 /app/cnn_model.h5 RUN chmod 666 /app/model.pkl RUN chmod 666 /app/target_encoder.pkl RUN chmod 666 /app/model_features.pkl # Ensure the 'uploads' directory is writable for runtime file saving RUN mkdir -p /app/uploads RUN chmod -R 777 /app/uploads # 7. Deployment Configuration ENV PORT=7860 EXPOSE 7860 # 8. Start app using Gunicorn CMD exec gunicorn --bind 0.0.0.0:$PORT --workers 1 --threads 8 app:app