File size: 2,274 Bytes
6e94f25
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ca27706
6e94f25
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ca27706
6e94f25
 
 
ca27706
6e94f25
 
 
 
 
31324b8
6e94f25
 
 
 
 
 
 
 
31324b8
 
 
6e94f25
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31324b8
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# ---------- Builder Stage ----------
FROM python:3.11-bookworm AS builder

WORKDIR /app

# Install system build dependencies
RUN apt-get update && apt-get install -y \
    build-essential \
    pkg-config \
    libhdf5-dev \
    libopenblas-dev \
    && apt-get clean && rm -rf /var/lib/apt/lists/*

# Install Python packages
COPY requirements.txt .
RUN pip install --no-cache-dir --upgrade pip setuptools wheel && \
    pip install --no-cache-dir -r requirements.txt

# Set Hugging Face cache environment variables
ENV HF_HOME=/app/hf_cache/huggingface
ENV HF_DATASETS_CACHE=/app/hf_cache/datasets

# Pre-download model
RUN python -c "from sentence_transformers import SentenceTransformer; SentenceTransformer('all-MiniLM-L6-v2')"

# ---------- Production Stage ----------
FROM python:3.11-bookworm AS production

# Create non-root user
RUN groupadd -r appuser && useradd -r -g appuser appuser

# Install runtime dependencies
RUN apt-get update && apt-get install -y --no-install-recommends \
    libgomp1 \
    libopenblas0 \
    libhdf5-103 \
    curl \
    && apt-get clean && rm -rf /var/lib/apt/lists/*

WORKDIR /app

# Copy Python environment from builder
COPY --from=builder /usr/local/lib/python3.11/site-packages /usr/local/lib/python3.11/site-packages
COPY --from=builder /usr/local/bin /usr/local/bin

# Copy Hugging Face cache from builder
COPY --from=builder /app/hf_cache /app/cache

# Copy application code
COPY . .

# Fix ownership of everything for non-root user
RUN mkdir -p \
    /app/database/faiss_index \
    /app/cache/datasets \
    /home/appuser/.cache/huggingface \
    /app/vector_store \
    /app/temp_vector_store && \
    chown -R appuser:appuser /app /home/appuser

# Important: Make faiss_index files writable
RUN chmod -R u+w /app/database/faiss_index

# Switch to non-root user
USER appuser

# Expose app port
EXPOSE 7860

# Health check
HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \
    CMD curl -f http://localhost:7860/health || exit 1

# Environment variables
ENV PYTHONPATH=/app \
    PYTHONUNBUFFERED=1 \
    PYTHONDONTWRITEBYTECODE=1 \
    HF_HOME=/app/cache/huggingface \
    HF_DATASETS_CACHE=/app/cache/datasets

# Launch FastAPI app
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]