Dockerfile 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. # Stage 1: Build the frontend
  2. FROM node:20-alpine AS frontend-builder
  3. WORKDIR /app/frontend
  4. # Copy only package files first for better caching
  5. COPY frontend/package*.json ./
  6. # Install dependencies
  7. RUN npm ci
  8. # Copy source code
  9. COPY frontend/ .
  10. # Build frontend
  11. RUN npm run build
  12. # Stage 2: Final image
  13. FROM python:3.10-slim
  14. WORKDIR /app
  15. # Set environment variables
  16. ENV PYTHONDONTWRITEBYTECODE=1
  17. ENV PYTHONUNBUFFERED=1
  18. ENV PYTHONPATH=/app
  19. # Install system dependencies including Redis server
  20. RUN apt-get update && apt-get install -y --no-install-recommends \
  21. build-essential \
  22. curl \
  23. redis-server \
  24. && rm -rf /var/lib/apt/lists/*
  25. # Configure pip mirror for faster downloads
  26. RUN pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
  27. # Copy requirements file and install dependencies
  28. COPY requirements.txt .
  29. RUN pip install --no-cache-dir -r requirements.txt \
  30. && apt-get purge -y --auto-remove build-essential \
  31. && rm -rf /var/lib/apt/lists/*
  32. # Copy built frontend assets from Stage 1
  33. COPY --from=frontend-builder /app/frontend/dist /app/frontend/dist
  34. # Copy the rest of the application code
  35. COPY . .
  36. # Create data directory
  37. RUN mkdir -p /app/data
  38. # Expose the port
  39. EXPOSE 8000
  40. # Create a startup script to run both Redis and Uvicorn
  41. # Configure Redis to use max 128MB memory and LRU eviction policy
  42. RUN echo '#!/bin/bash\n\
  43. redis-server --daemonize yes --maxmemory 128mb --maxmemory-policy allkeys-lru\n\
  44. python -m uvicorn app.main:app --host 0.0.0.0 --port 8000' > /app/start.sh && chmod +x /app/start.sh
  45. # Default command to run the application
  46. CMD ["/app/start.sh"]