| # Multi-stage Dockerfile for MKCart Fullstack App | |
| # Optimized for Hugging Face Spaces | |
| # Stage 1: Build Frontend | |
| FROM node:20-alpine AS frontend-builder | |
| WORKDIR /app/frontend | |
| # Copy package files first for better caching | |
| COPY frontend/package*.json ./ | |
| RUN npm install | |
| # Copy source and build | |
| COPY frontend/ ./ | |
| # Set REACT_APP_API_URL to empty to use relative paths (serving from same origin) | |
| ENV REACT_APP_API_URL="" | |
| RUN npm run build | |
| # Stage 2: Final Runner | |
| FROM node:20-alpine | |
| WORKDIR /app | |
| # Install backend dependencies | |
| COPY backend/package*.json ./backend/ | |
| WORKDIR /app/backend | |
| RUN npm install --production | |
| # Copy backend source | |
| COPY backend/ ./ | |
| # Copy built frontend from Stage 1 | |
| # Backend expects it at ../frontend/build | |
| COPY --from=frontend-builder /app/frontend/build /app/frontend/build | |
| # Environment configuration | |
| ENV NODE_ENV=production | |
| ENV PORT=7860 | |
| # HF Spaces expects the app to be reachable on port 7860 | |
| EXPOSE 7860 | |
| # Health check (optional but good for HF) | |
| HEALTHCHECK --interval=30s --timeout=3s \ | |
| CMD wget --quiet --tries=1 --spider http://localhost:7860/api/products || exit 1 | |
| # Start the application | |
| CMD ["node", "app.js"] | |