# Dockerfile - EasyOCR Tuning REST API
#
# Build:
#   docker build -t easyocr-api:latest .
#
# Run:
#   docker run -p 8002:8000 -v ./dataset:/app/dataset easyocr-api:latest

FROM python:3.11-slim

LABEL maintainer="Sergio Jimenez"
LABEL description="EasyOCR Tuning REST API"

WORKDIR /app

# Set environment variables
ENV PYTHONUNBUFFERED=1
ENV EASYOCR_LANGUAGES=es,en

# Install system dependencies for OpenCV and image processing
RUN apt-get update && apt-get install -y --no-install-recommends \
    libgl1 \
    libglib2.0-0 \
    libsm6 \
    libxext6 \
    libxrender1 \
    && rm -rf /var/lib/apt/lists/*

# Copy and install Python dependencies
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt

# Copy application code
COPY easyocr_tuning_rest.py .
COPY dataset_manager.py .

# Volume for dataset and model cache
VOLUME ["/app/dataset", "/root/.EasyOCR"]

# Expose API port
EXPOSE 8000

# Health check
HEALTHCHECK --interval=30s --timeout=10s --start-period=120s --retries=3 \
    CMD python -c "import urllib.request; urllib.request.urlopen('http://localhost:8000/health')" || exit 1

# Run the API server
CMD ["uvicorn", "easyocr_tuning_rest:app", "--host", "0.0.0.0", "--port", "8000"]
