Dockerfile 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. # Multi-stage Dockerfile for ARM64 optimization
  2. # Stage 1: Build dependencies + PyArmor obfuscation
  3. ENV CODENAME=`lsb_release -a | grep Codename | awk '{ print $2 }'`
  4. FROM python:3.11-slim-"$CODENAME" as builder
  5. # Install system dependencies for building
  6. RUN apt-get update && apt-get install -y \
  7. build-essential \
  8. curl \
  9. && rm -rf /var/lib/apt/lists/*
  10. # Create virtual environment
  11. RUN python -m venv /opt/venv
  12. ENV PATH="/opt/venv/bin:$PATH"
  13. # Copy requirements and install Python dependencies + PyArmor
  14. COPY requirements.txt .
  15. RUN pip install --no-cache-dir --upgrade pip && \
  16. pip install --no-cache-dir -r requirements.txt && \
  17. pip install --no-cache-dir pyarmor
  18. # Copy application code for obfuscation
  19. COPY . /build
  20. WORKDIR /build
  21. # Fix CRLF + register license + obfuscate
  22. # Skip if WAOUW_SKIP_OBFUSCATION=1 (for development/debugging)
  23. ARG WAOUW_SKIP_OBFUSCATION=0
  24. RUN find /build -name "*.py" -exec sed -i 's/\r$//' {} + && \
  25. find /build -name "*.sh" -exec sed -i 's/\r$//' {} + && \
  26. if [ "$WAOUW_SKIP_OBFUSCATION" != "1" ]; then \
  27. bash /build/obfuscate.sh 2>&1 || echo "Obfuscation failed, continuing with plain .py"; \
  28. fi
  29. # Production stage
  30. FROM python:3.11-slim-bullseye
  31. # Install runtime dependencies (curl, Docker CLI for exec, SMB/NFS for backup)
  32. RUN apt-get update && apt-get install -y \
  33. curl \
  34. cifs-utils \
  35. nfs-common \
  36. ca-certificates \
  37. gnupg \
  38. && install -m 0755 -d /etc/apt/keyrings \
  39. && curl -fsSL https://download.docker.com/linux/debian/gpg | gpg --dearmor -o /etc/apt/keyrings/docker.gpg \
  40. && echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/debian bullseye stable" > /etc/apt/sources.list.d/docker.list \
  41. && apt-get update && apt-get install -y docker-ce-cli \
  42. && rm -rf /var/lib/apt/lists/*
  43. # Create non-root user
  44. RUN groupadd -r waouw && useradd -r -g waouw waouw
  45. # Copy virtual environment from builder
  46. COPY --from=builder /opt/venv /opt/venv
  47. ENV PATH="/opt/venv/bin:$PATH"
  48. # Set working directory
  49. WORKDIR /app
  50. # Copy obfuscated application code from builder
  51. COPY --from=builder --chown=waouw:waouw /build .
  52. # Remove build artifacts and Docker marker (PyArmor license check)
  53. RUN rm -f compile_cython.py obfuscate.sh pyarmor-regkey.txt && \
  54. rm -rf build/ __pycache__/ && \
  55. find . -name "*.c" -delete && \
  56. find . -name "_cython_setup_*" -delete && \
  57. rm -f /.dockerenv
  58. # Create directories for logs and data
  59. RUN mkdir -p /app/logs /app/data /data/backups && \
  60. chown -R waouw:waouw /app /data/backups
  61. # Switch to non-root user
  62. USER waouw
  63. # Health check
  64. HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \
  65. CMD curl -f http://localhost:8000/health || exit 1
  66. # Expose port
  67. EXPOSE 8000
  68. # Start application
  69. CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000", "--workers", "1"]