mfoud444 commited on
Commit
ca33059
·
verified ·
1 Parent(s): dab7e8e

Update Dockerfile

Browse files
Files changed (1) hide show
  1. Dockerfile +71 -8
Dockerfile CHANGED
@@ -1,13 +1,76 @@
1
- FROM python:3.9
2
 
3
- RUN useradd -m -u 1000 user
4
- USER user
5
- ENV PATH="/home/user/.local/bin:$PATH"
 
 
 
6
 
7
  WORKDIR /app
8
 
9
- COPY --chown=user ./requirements.txt requirements.txt
10
- RUN pip install --no-cache-dir --upgrade -r requirements.txt
 
 
 
 
11
 
12
- COPY --chown=user . /app
13
- CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.11-slim
2
 
3
+ # Set environment variables
4
+ ENV PYTHONUNBUFFERED=1 \
5
+ PYTHONDONTWRITEBYTECODE=1 \
6
+ ENV_MODE="production" \
7
+ PYTHONPATH=/app \
8
+ REDIS_URL="redis://localhost:6379/0"
9
 
10
  WORKDIR /app
11
 
12
+ # Install system dependencies including Redis
13
+ RUN apt-get update && apt-get install -y --no-install-recommends \
14
+ build-essential \
15
+ curl \
16
+ redis-server \
17
+ && rm -rf /var/lib/apt/lists/*
18
 
19
+ # Configure Redis
20
+ RUN sed -i 's/bind 127.0.0.1 ::1/bind 0.0.0.0/' /etc/redis/redis.conf && \
21
+ sed -i 's/protected-mode yes/protected-mode no/' /etc/redis/redis.conf && \
22
+ sed -i 's/daemonize yes/daemonize no/' /etc/redis/redis.conf
23
+
24
+ # Create non-root user and set up directories
25
+ RUN useradd -m -u 1000 appuser && \
26
+ mkdir -p /app/logs && \
27
+ chown -R appuser:appuser /app && \
28
+ chown -R appuser:appuser /var/lib/redis && \
29
+ chown -R appuser:appuser /var/log/redis && \
30
+ chown -R appuser:appuser /etc/redis
31
+
32
+ # Install Python dependencies
33
+ COPY --chown=appuser:appuser requirements.txt .
34
+ RUN pip install --no-cache-dir -r requirements.txt gunicorn
35
+
36
+ # Switch to non-root user
37
+ USER appuser
38
+
39
+ # Copy application code
40
+ COPY --chown=appuser:appuser . .
41
+
42
+ # Expose the port the app runs on
43
+ EXPOSE 7860 6379
44
+
45
+ # Calculate optimal worker count based on 16 vCPUs
46
+ # Using (2*CPU)+1 formula for CPU-bound applications
47
+ ENV WORKERS=33
48
+ ENV THREADS=2
49
+ ENV WORKER_CONNECTIONS=2000
50
+
51
+ # Create a startup script to run both Redis and Gunicorn
52
+ RUN echo '#!/bin/sh\n\
53
+ redis-server /etc/redis/redis.conf &\n\
54
+ gunicorn api:app \
55
+ --workers $WORKERS \
56
+ --worker-class uvicorn.workers.UvicornWorker \
57
+ --bind 0.0.0.0:7860 \
58
+ --timeout 600 \
59
+ --graceful-timeout 300 \
60
+ --keep-alive 250 \
61
+ --max-requests 2000 \
62
+ --max-requests-jitter 400 \
63
+ --forwarded-allow-ips '*' \
64
+ --worker-connections $WORKER_CONNECTIONS \
65
+ --worker-tmp-dir /dev/shm \
66
+ --preload \
67
+ --log-level info \
68
+ --access-logfile - \
69
+ --error-logfile - \
70
+ --capture-output \
71
+ --enable-stdio-inheritance \
72
+ --threads $THREADS\n\
73
+ wait' > /app/start.sh && chmod +x /app/start.sh
74
+
75
+ # Run the startup script
76
+ CMD ["/app/start.sh"]