diff --git a/workloads/ecommerce/Dockerfile b/workloads/ecommerce/Dockerfile index a7a351c..c70a46f 100644 --- a/workloads/ecommerce/Dockerfile +++ b/workloads/ecommerce/Dockerfile @@ -3,7 +3,7 @@ ARG NODE_IMAGE=node:24.18.0-bookworm-slim # ============================================================ -# Dependencias +# 1. Instalación de dependencias # ============================================================ FROM ${NODE_IMAGE} AS deps @@ -12,37 +12,57 @@ WORKDIR /app ENV NEXT_TELEMETRY_DISABLED=1 \ NPM_CONFIG_AUDIT=false \ NPM_CONFIG_FUND=false \ - NPM_CONFIG_FETCH_RETRIES=5 \ - NPM_CONFIG_FETCH_RETRY_MINTIMEOUT=20000 \ - NPM_CONFIG_FETCH_RETRY_MAXTIMEOUT=120000 \ + NPM_CONFIG_FETCH_RETRIES=3 \ + NPM_CONFIG_FETCH_RETRY_MINTIMEOUT=10000 \ + NPM_CONFIG_FETCH_RETRY_MAXTIMEOUT=60000 \ NPM_CONFIG_FETCH_TIMEOUT=120000 \ - NPM_CONFIG_LOGLEVEL=warn + NPM_CONFIG_MAXSOCKETS=6 \ + NPM_CONFIG_LOGLEVEL=info COPY package.json package-lock.json ./ -RUN --mount=type=cache,target=/root/.npm,sharing=locked \ - npm ci \ +# Muestra las versiones y valida acceso al registro antes de instalar. +RUN node --version \ + && npm --version \ + && npm config set registry https://registry.npmjs.org/ \ + && npm config get registry \ + && npm ping + +# sharing=private evita quedar esperando el caché de otro build. +# timeout impide que npm quede bloqueado indefinidamente. +RUN --mount=type=cache,target=/root/.npm,sharing=private \ + timeout 600s npm ci \ --no-audit \ --no-fund \ - --prefer-offline + --prefer-offline \ + --loglevel=info \ + --timing + # ============================================================ -# Compilación Next.js +# 2. Compilación de Next.js # ============================================================ FROM ${NODE_IMAGE} AS builder WORKDIR /app -ENV NEXT_TELEMETRY_DISABLED=1 \ - NODE_ENV=production +ENV NODE_ENV=production \ + NEXT_TELEMETRY_DISABLED=1 \ + NODE_OPTIONS=--max-old-space-size=2048 COPY --from=deps /app/node_modules ./node_modules COPY . . -RUN npm run build +# Conserva el caché de compilación de Next.js entre builds. +RUN --mount=type=cache,target=/app/.next/cache,sharing=private \ + npm run build + +# Verifica que next.config.ts tenga output: "standalone". +RUN test -f /app/.next/standalone/server.js + # ============================================================ -# Imagen final +# 3. Imagen final de producción # ============================================================ FROM ${NODE_IMAGE} AS runner @@ -53,18 +73,18 @@ ENV NODE_ENV=production \ HOSTNAME=0.0.0.0 \ PORT=80 +# libcap permite que el usuario no-root escuche en el puerto 80. RUN apt-get update \ && apt-get install -y --no-install-recommends \ ca-certificates \ libcap2-bin \ - wget \ && rm -rf /var/lib/apt/lists/* \ - && groupadd --system --gid 1001 nodejs \ + && groupadd --gid 1001 nodejs \ && useradd \ - --system \ --uid 1001 \ - --gid nodejs \ + --gid 1001 \ --create-home \ + --shell /usr/sbin/nologin \ nextjs \ && setcap 'cap_net_bind_service=+ep' /usr/local/bin/node @@ -89,9 +109,9 @@ EXPOSE 80 HEALTHCHECK \ --interval=30s \ - --timeout=5s \ - --start-period=20s \ + --timeout=8s \ + --start-period=30s \ --retries=3 \ - CMD wget -qO- http://127.0.0.1/api/health || exit 1 + CMD ["node", "-e", "fetch('http://127.0.0.1/api/health').then(r => process.exit(r.ok ? 0 : 1)).catch(() => process.exit(1))"] CMD ["node", "server.js"] \ No newline at end of file