Dockerfile 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. # Stage 1: Build Frontend
  2. FROM node:22 AS frontend-builder
  3. WORKDIR /app/frontend
  4. # 使用淘宝 NPM 镜像
  5. RUN npm config set registry https://registry.npmmirror.com
  6. RUN npm config set fetch-retry-maxtimeout 120000
  7. RUN npm config set fetch-retry-mintimeout 20000
  8. RUN npm config set fetch-retries 5
  9. COPY frontend/package*.json ./
  10. RUN rm -f package-lock.json
  11. RUN npm install
  12. COPY frontend/ ./
  13. # Build the frontend. The output should be in frontend/dist
  14. # 由于 vite.config.ts 设置了 outDir 为 ../backend/dist,这里我们需要强制覆盖为 dist
  15. RUN npm run build -- --outDir dist
  16. RUN ls -la dist/
  17. # Stage 2: Backend Runtime
  18. FROM python:3.10-slim
  19. WORKDIR /app
  20. ARG DEBIAN_FRONTEND=noninteractive
  21. # 替换 apt 源为阿里云源 (Debian Bookworm)
  22. RUN echo "deb https://mirrors.aliyun.com/debian/ bookworm main non-free non-free-firmware contrib" > /etc/apt/sources.list && \
  23. echo "deb-src https://mirrors.aliyun.com/debian/ bookworm main non-free non-free-firmware contrib" >> /etc/apt/sources.list && \
  24. echo "deb https://mirrors.aliyun.com/debian-security/ bookworm-security main" >> /etc/apt/sources.list && \
  25. echo "deb-src https://mirrors.aliyun.com/debian-security/ bookworm-security main" >> /etc/apt/sources.list && \
  26. echo "deb https://mirrors.aliyun.com/debian/ bookworm-updates main non-free non-free-firmware contrib" >> /etc/apt/sources.list && \
  27. echo "deb-src https://mirrors.aliyun.com/debian/ bookworm-updates main non-free non-free-firmware contrib" >> /etc/apt/sources.list && \
  28. echo "deb https://mirrors.aliyun.com/debian/ bookworm-backports main non-free non-free-firmware contrib" >> /etc/apt/sources.list && \
  29. echo "deb-src https://mirrors.aliyun.com/debian/ bookworm-backports main non-free non-free-firmware contrib" >> /etc/apt/sources.list
  30. # Install system dependencies required for OpenCV
  31. RUN apt-get update && apt-get install -y \
  32. libgl1 \
  33. libglib2.0-0 \
  34. && rm -rf /var/lib/apt/lists/*
  35. # Install Python dependencies
  36. COPY backend/requirements.txt .
  37. # 使用清华 PyPI 镜像
  38. RUN pip install --upgrade pip
  39. RUN pip install --no-cache-dir -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple
  40. # Copy backend code
  41. COPY backend/ ./backend/
  42. COPY run.py .
  43. # Copy built frontend assets to backend/dist (as expected by main.py)
  44. COPY --from=frontend-builder /app/frontend/dist ./backend/dist
  45. # Create directories for volumes
  46. RUN mkdir -p reports backend/app/static/snapshots
  47. # Expose the port
  48. EXPOSE 8000
  49. # Run the application
  50. CMD ["python", "run.py"]