2025-04-18 10:22:35 +08:00
|
|
|
FROM python:3.11-slim
|
|
|
|
|
2025-04-28 05:21:59 +08:00
|
|
|
# Set environment variables
|
|
|
|
ENV PYTHONUNBUFFERED=1 \
|
|
|
|
PYTHONDONTWRITEBYTECODE=1 \
|
|
|
|
ENV_MODE="production" \
|
|
|
|
PYTHONPATH=/app
|
2025-04-25 04:38:10 +08:00
|
|
|
|
2025-04-28 05:21:59 +08:00
|
|
|
WORKDIR /app
|
2025-04-25 05:15:40 +08:00
|
|
|
|
2025-04-28 05:21:59 +08:00
|
|
|
# Install system dependencies
|
|
|
|
RUN apt-get update && apt-get install -y --no-install-recommends \
|
|
|
|
build-essential \
|
|
|
|
curl \
|
|
|
|
&& rm -rf /var/lib/apt/lists/*
|
2025-04-18 10:22:35 +08:00
|
|
|
|
2025-04-28 05:21:59 +08:00
|
|
|
# Create non-root user and set up directories
|
|
|
|
RUN useradd -m -u 1000 appuser && \
|
|
|
|
mkdir -p /app/logs && \
|
|
|
|
chown -R appuser:appuser /app
|
2025-04-18 10:22:35 +08:00
|
|
|
|
2025-04-28 05:21:59 +08:00
|
|
|
# Install Python dependencies
|
|
|
|
COPY --chown=appuser:appuser requirements.txt .
|
|
|
|
RUN pip install --no-cache-dir -r requirements.txt gunicorn
|
2025-04-18 10:22:35 +08:00
|
|
|
|
2025-04-28 05:21:59 +08:00
|
|
|
# Switch to non-root user
|
|
|
|
USER appuser
|
2025-04-25 04:38:10 +08:00
|
|
|
|
2025-04-28 05:21:59 +08:00
|
|
|
# Copy application code
|
|
|
|
COPY --chown=appuser:appuser . .
|
2025-04-24 08:45:58 +08:00
|
|
|
|
2025-04-18 10:22:35 +08:00
|
|
|
# Expose the port the app runs on
|
|
|
|
EXPOSE 8000
|
|
|
|
|
2025-04-28 05:21:59 +08:00
|
|
|
# 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
|
|
|
|
|
|
|
|
# Gunicorn configuration
|
|
|
|
CMD ["sh", "-c", "gunicorn api:app \
|
|
|
|
--workers $WORKERS \
|
|
|
|
--worker-class uvicorn.workers.UvicornWorker \
|
|
|
|
--bind 0.0.0.0:8000 \
|
2025-05-12 00:58:42 +08:00
|
|
|
--timeout 1800 \
|
|
|
|
--graceful-timeout 600 \
|
|
|
|
--keep-alive-timeout 1800 \
|
2025-04-28 05:21:59 +08:00
|
|
|
--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"]
|