# Multi-stage build for TypeScript Node.js backend FROM node:20-alpine AS builder WORKDIR /app # Copy package files COPY package*.json ./ COPY tsconfig.json ./ # Install dependencies (including dev dependencies for build) RUN npm ci # Copy source code COPY . . # Build TypeScript RUN npm run build # Production stage FROM node:20-alpine AS production WORKDIR /app # Install production dependencies only COPY package*.json ./ RUN npm ci --only=production && npm cache clean --force # Copy built files from builder COPY --from=builder /app/dist ./dist # Copy source route files for Swagger JSDoc (needs TS comments for documentation) COPY --from=builder /app/src/routes ./src/routes # Create non-root user for security RUN addgroup -g 1001 -S nodejs && \ adduser -S nodejs -u 1001 # Change ownership RUN chown -R nodejs:nodejs /app USER nodejs # Expose port (Hugging Face Spaces uses 7860 by default) EXPOSE 7860 # Health check HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \ CMD node -e "require('http').get('http://localhost:7860/health', (r) => {process.exit(r.statusCode === 200 ? 0 : 1)})" # Start the application CMD ["node", "dist/server.js"]