File size: 1,379 Bytes
ce1b49c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5ff3439
 
 
ce1b49c
 
 
 
 
5ff3439
 
ce1b49c
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
FROM python:3.14-slim

WORKDIR /app

# Security and performance environment variables
ENV PYTHONDONTWRITEBYTECODE=1 \
    PYTHONUNBUFFERED=1 \
    PIP_NO_CACHE_DIR=1 \
    PIP_DISABLE_PIP_VERSION_CHECK=1

# Install security updates
RUN apt-get update && \
    apt-get upgrade -y && \
    apt-get clean && \
    rm -rf /var/lib/apt/lists/*

# Create non-root user with specific UID
RUN useradd -m -u 1000 -s /bin/bash user

# Install dependencies as root
COPY requirements.txt .
RUN pip install --no-cache-dir --upgrade pip && \
    pip install --no-cache-dir -r requirements.txt

# Copy application code and set ownership
COPY --chown=user:user . .

# Switch to non-root user
USER user

# Set PATH for user
ENV PATH="/home/user/.local/bin:$PATH" \
    HOME="/home/user"

# Health check (respect Hugging Face PORT env)
HEALTHCHECK --interval=30s --timeout=10s --start-period=15s --retries=3 \
    CMD python3 -c "import os, requests; port=os.environ.get('PORT','7860'); requests.get(f'http://localhost:{port}/health', timeout=5)"

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

# Start server with production settings
# Note: default workers=1 is safer on low-memory HF hardware; override via WEB_CONCURRENCY.
CMD ["sh", "-c", "uvicorn server:app --host 0.0.0.0 --port ${PORT:-7860} --log-level ${LOG_LEVEL:-info} --no-access-log --workers ${WEB_CONCURRENCY:-1}"]