# ============================================================================= # DEPS Stage: Install dependencies only (cacheable separately) # ============================================================================= FROM node:22-alpine AS deps RUN corepack enable && corepack prepare pnpm@9.0.0 --activate WORKDIR /app # Copiar SOLO los archivos de dependencias (máxima reutilización de caché) COPY package.json pnpm-lock.yaml ./ # Instalar dependencias con caché de pnpm persistente RUN --mount=type=cache,id=pnpm,target=/pnpm/store \ pnpm install --frozen-lockfile --aggregate-output --store-dir /pnpm/store # ============================================================================= # Build Stage: Compile aplicación # ============================================================================= FROM node:22-alpine AS builder # Arguments for construction ARG VITE_API_BASE ENV VITE_API_BASE=$VITE_API_BASE ARG VITE_ADMIN_APP_ORIGIN ENV VITE_ADMIN_APP_ORIGIN=$VITE_ADMIN_APP_ORIGIN ARG VITE_MOBILE_APP_ORIGIN ENV VITE_MOBILE_APP_ORIGIN=$VITE_MOBILE_APP_ORIGIN ARG VITE_API_URL ENV VITE_API_URL=$VITE_API_URL RUN corepack enable && corepack prepare pnpm@9.0.0 --activate WORKDIR /app # Copiar node_modules desde stage deps (evita reinstalar) COPY --from=deps /app/node_modules ./node_modules # Copy source code COPY . . # Build for production RUN pnpm build # ============================================================================= # RUNTIME Stage (Nginx) # ============================================================================= FROM nginx:alpine COPY --from=builder /app/dist /usr/share/nginx/html # Optimized Nginx config for PWA (with API proxy and Cache Control) RUN echo 'server { \ listen 80; \ root /usr/share/nginx/html; \ index index.html; \ \ # Cache Control para el index.html y sw.js (NUNCA cachear) \ location ~* ^/(index\.html|sw\.js)$ { \ add_header Cache-Control "no-store, no-cache, must-revalidate, proxy-revalidate, max-age=0"; \ } \ \ # Cache Control para assets estáticos (Vite usa hashes) \ location /assets/ { \ expires 1y; \ add_header Cache-Control "public, immutable"; \ } \ \ location / { \ try_files $uri $uri/ /index.html; \ } \ \ location /api/ { \ resolver 127.0.0.11 valid=30s; \ set $backend_upstream backend-api; \ proxy_pass http://$backend_upstream:8000; \ proxy_http_version 1.1; \ proxy_set_header Upgrade $http_upgrade; \ proxy_set_header Connection "upgrade"; \ proxy_set_header Host $host; \ } \ }' > /etc/nginx/conf.d/default.conf EXPOSE 80 CMD ["nginx", "-g", "daemon off;"]