File size: 1,655 Bytes
b88d8cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9b9a454
 
b88d8cd
 
 
 
 
 
 
9b9a454
 
b88d8cd
 
 
 
 
 
 
9b9a454
b88d8cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# CloverShield ML Inference API Dockerfile
# Optimized for Python 3.9 and Hugging Face Spaces

FROM python:3.9-slim

# Create a non-root user (user 1000) to comply with HF security context
RUN useradd -m -u 1000 user

# Set working directory
WORKDIR /app

# Set environment variables
ENV PYTHONUNBUFFERED=1 \
    PYTHONDONTWRITEBYTECODE=1 \
    PIP_NO_CACHE_DIR=1 \
    PIP_DISABLE_PIP_VERSION_CHECK=1 \
    PIP_DEFAULT_TIMEOUT=100 \
    MAX_FIT_ROWS=10000 \
    HOME=/home/user \
    PATH=/home/user/.local/bin:$PATH

# Install system dependencies
RUN apt-get update && apt-get install -y \
    gcc \
    g++ \
    gfortran \
    libopenblas-dev \
    && rm -rf /var/lib/apt/lists/*

# Copy requirements first (for better Docker layer caching)
COPY requirements.txt .

# Install Python dependencies as the non-root user
RUN pip install --upgrade pip && \
    pip install --no-cache-dir --prefer-binary -r requirements.txt

# Copy application code
COPY --chown=user . .

# Create directory for model (if not exists) and ensure permissions
RUN mkdir -p Models && chown -R user:user /app

# Copy and set up entrypoint script
COPY --chown=user entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh

# Switch to the non-root user
USER user

# Expose port 7860 (Hugging Face Spaces standard)
EXPOSE 7860

# Health check (uses PORT env var, defaults to 7860)
HEALTHCHECK --interval=30s --timeout=10s --start-period=60s --retries=3 \
    CMD python -c "import os, urllib.request; port=os.getenv('PORT', '7860'); urllib.request.urlopen(f'http://localhost:{port}/health')" || exit 1

# Run the application using entrypoint script
ENTRYPOINT ["/entrypoint.sh"]