File size: 2,158 Bytes
dab7e8e
a51a15b
dab7e8e
 
 
 
 
 
a51a15b
dab7e8e
a51a15b
dab7e8e
 
 
 
 
 
a51a15b
dab7e8e
 
 
 
a51a15b
dab7e8e
 
 
 
 
 
 
a51a15b
dab7e8e
 
 
a51a15b
dab7e8e
 
a51a15b
dab7e8e
 
a51a15b
dab7e8e
 
a51a15b
dab7e8e
 
 
 
 
a51a15b
dab7e8e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a51a15b
dab7e8e
 
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
FROM python:3.11-slim

# Set environment variables
ENV PYTHONUNBUFFERED=1 \
    PYTHONDONTWRITEBYTECODE=1 \
    ENV_MODE="production" \
    PYTHONPATH=/app \
    REDIS_URL="redis://localhost:6379/0"

WORKDIR /app

# Install system dependencies including Redis
RUN apt-get update && apt-get install -y --no-install-recommends \
    build-essential \
    curl \
    redis-server \
    && rm -rf /var/lib/apt/lists/*

# Configure Redis
RUN sed -i 's/bind 127.0.0.1 ::1/bind 0.0.0.0/' /etc/redis/redis.conf && \
    sed -i 's/protected-mode yes/protected-mode no/' /etc/redis/redis.conf && \
    sed -i 's/daemonize yes/daemonize no/' /etc/redis/redis.conf

# Create non-root user and set up directories
RUN useradd -m -u 1000 appuser && \
    mkdir -p /app/logs && \
    chown -R appuser:appuser /app && \
    chown -R appuser:appuser /var/lib/redis && \
    chown -R appuser:appuser /var/log/redis && \
    chown -R appuser:appuser /etc/redis

# Install Python dependencies
COPY --chown=appuser:appuser requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt gunicorn

# Switch to non-root user
USER appuser

# Copy application code
COPY --chown=appuser:appuser . .

# Expose the port the app runs on
EXPOSE 7860 6379

# Calculate optimal worker count based on 16 vCPUs
# Using (2*CPU)+1 formula for CPU-bound applications
ENV WORKERS=33
ENV THREADS=2
ENV WORKER_CONNECTIONS=2000

# Create a startup script to run both Redis and Gunicorn
RUN echo '#!/bin/sh\n\
redis-server /etc/redis/redis.conf &\n\
gunicorn api:app \
     --workers $WORKERS \
     --worker-class uvicorn.workers.UvicornWorker \
     --bind 0.0.0.0:7860 \
     --timeout 600 \
     --graceful-timeout 300 \
     --keep-alive 250 \
     --max-requests 2000 \
     --max-requests-jitter 400 \
     --forwarded-allow-ips '*' \
     --worker-connections $WORKER_CONNECTIONS \
     --worker-tmp-dir /dev/shm \
     --preload \
     --log-level info \
     --access-logfile - \
     --error-logfile - \
     --capture-output \
     --enable-stdio-inheritance \
     --threads $THREADS\n\
wait' > /app/start.sh && chmod +x /app/start.sh

# Run the startup script
CMD ["/app/start.sh"]