# WidgeTDC Backend - GPU-Enabled Production Dockerfile # Optimized for Hugging Face Spaces with NVIDIA GPU support # BUILD_VERSION: 2024-12-11-v3 # # NOTE: This Dockerfile is used by the HF deployment workflow. # The build context is the hf-space/ directory created by the workflow. FROM nvidia/cuda:12.2.0-runtime-ubuntu22.04 AS base # Install Node.js 20.x and build dependencies RUN apt-get update && apt-get install -y \ curl \ git \ python3 \ python3-pip \ build-essential \ && curl -fsSL https://deb.nodesource.com/setup_20.x | bash - \ && apt-get install -y nodejs \ && rm -rf /var/lib/apt/lists/* WORKDIR /app # CRITICAL: Upgrade pip FIRST to fix resolver bug (AssertionError in get_topological_weights) RUN python3 -m pip install --upgrade pip setuptools wheel # Install Python ML dependencies one by one to avoid resolver issues RUN pip3 install --no-cache-dir torch==2.1.2 RUN pip3 install --no-cache-dir transformers==4.36.0 RUN pip3 install --no-cache-dir sentence-transformers==2.3.1 RUN pip3 install --no-cache-dir accelerate==0.25.0 # Copy all package files first for better caching # Root package.json contains workspaces config COPY package.json package-lock.json* ./ # Create directory structure and copy package files RUN mkdir -p apps/backend packages/domain-types packages/mcp-types COPY apps/backend/package.json ./apps/backend/ COPY packages/domain-types/package.json ./packages/domain-types/ COPY packages/mcp-types/package.json ./packages/mcp-types/ # Install all dependencies (including dev for building) RUN npm install --legacy-peer-deps || npm install --legacy-peer-deps --force # Copy source files COPY packages/domain-types/ ./packages/domain-types/ COPY packages/mcp-types/ ./packages/mcp-types/ COPY apps/backend/ ./apps/backend/ COPY tsconfig.json ./ # Build shared packages WORKDIR /app/packages/domain-types RUN npm run build || echo "domain-types build skipped" WORKDIR /app/packages/mcp-types RUN npm run build || echo "mcp-types build skipped" # Generate Prisma client and build backend WORKDIR /app/apps/backend RUN npx prisma generate || echo "Prisma generate skipped" RUN npm run build # Production stage - minimal runtime image FROM nvidia/cuda:12.2.0-runtime-ubuntu22.04 AS production # Install Node.js runtime RUN apt-get update && apt-get install -y \ curl \ python3 \ python3-pip \ && curl -fsSL https://deb.nodesource.com/setup_20.x | bash - \ && apt-get install -y nodejs \ && rm -rf /var/lib/apt/lists/* WORKDIR /app # CRITICAL: Upgrade pip FIRST RUN python3 -m pip install --upgrade pip setuptools wheel # Install Python ML dependencies one by one RUN pip3 install --no-cache-dir torch==2.1.2 RUN pip3 install --no-cache-dir transformers==4.36.0 RUN pip3 install --no-cache-dir sentence-transformers==2.3.1 RUN pip3 install --no-cache-dir accelerate==0.25.0 # Copy necessary files from build stage COPY --from=base /app/package.json ./ COPY --from=base /app/node_modules ./node_modules COPY --from=base /app/packages ./packages COPY --from=base /app/apps/backend/dist ./apps/backend/dist COPY --from=base /app/apps/backend/package.json ./apps/backend/ COPY --from=base /app/apps/backend/prisma ./apps/backend/prisma # GPU Configuration ENV NVIDIA_VISIBLE_DEVICES=all ENV NVIDIA_DRIVER_CAPABILITIES=compute,utility ENV CUDA_VISIBLE_DEVICES=0 # App Configuration ENV NODE_ENV=production ENV PORT=7860 ENV USE_GPU=true ENV TRANSFORMERS_CACHE=/app/.cache ENV HF_HOME=/app/.cache # Create cache directory for models RUN mkdir -p /app/.cache && chmod 777 /app/.cache # Hugging Face Spaces requires port 7860 EXPOSE 7860 WORKDIR /app/apps/backend # Health check HEALTHCHECK --interval=30s --timeout=10s --start-period=60s --retries=3 \ CMD curl -f http://localhost:7860/health || exit 1 # Start with GPU support enabled CMD ["node", "--max-old-space-size=4096", "--expose-gc", "dist/index.js"]