From 7b88426f9648fe9e4921fcefbfeca6135ecdba51 Mon Sep 17 00:00:00 2001 From: mukeshs Date: Mon, 23 Feb 2026 11:41:26 +0530 Subject: [PATCH] package update --- DockerFile | 76 ++++++++++++++++++++++++++++++-------------------- healthcheck.js | 18 ++++++++++++ 2 files changed, 63 insertions(+), 31 deletions(-) create mode 100644 healthcheck.js diff --git a/DockerFile b/DockerFile index f5dc9b6..9f8b2bb 100644 --- a/DockerFile +++ b/DockerFile @@ -1,55 +1,69 @@ -# Stage 1: Build -# Change from node:18-alpine to node:22-alpine -FROM node:22-alpine AS builder +# Multi-stage build for Next.js frontend +FROM node:22 AS base +# Install dependencies only when needed +FROM base AS deps WORKDIR /app -# Build arguments - MUST be before COPY to be available during build -ARG NEXT_PUBLIC_API_URL -ENV NEXT_PUBLIC_API_URL=${NEXT_PUBLIC_API_URL} +COPY package.json yarn.lock* package-lock.json* pnpm-lock.yaml* ./ +RUN \ + if [ -f yarn.lock ]; then yarn --frozen-lockfile; \ + elif [ -f package-lock.json ]; then npm ci; \ + elif [ -f pnpm-lock.yaml ]; then corepack enable && pnpm i --frozen-lockfile; \ + else echo "Lockfile not found." && exit 1; \ + fi -# Copy package files -COPY package.json package-lock.json ./ +# Rebuild the source code only when needed +FROM base AS builder +WORKDIR /app -# Install ALL dependencies (including dev dependencies for build) -RUN npm ci - -# Copy source code +COPY --from=deps /app/node_modules ./node_modules COPY . . -# Build the app +ENV NEXT_TELEMETRY_DISABLED=1 + +ARG NEXT_PUBLIC_API_URL +ARG NODE_ENV=production + +ENV NEXT_PUBLIC_API_URL=${NEXT_PUBLIC_API_URL} +ENV NODE_ENV=${NODE_ENV} + RUN npm run build -# Stage 2: Run -# Change from node:18-alpine to node:22-alpine -FROM node:22-alpine AS runner - +# Production image +FROM base AS runner WORKDIR /app -# Set to production -ENV NODE_ENV=production - -# Runtime environment variable ARG NEXT_PUBLIC_API_URL ENV NEXT_PUBLIC_API_URL=${NEXT_PUBLIC_API_URL} +ENV NODE_ENV=production +ENV NEXT_TELEMETRY_DISABLED=1 -# Copy necessary files from the standalone build +# Create non-root user +RUN addgroup --system --gid 1001 nodejs && \ + adduser --system --uid 1001 nextjs + +# Copy public folder COPY --from=builder /app/public ./public -COPY --from=builder /app/.next/standalone ./ -COPY --from=builder /app/.next/static ./.next/static -# Create cache directory with proper permissions -RUN mkdir -p .next/cache/images && \ - chmod -R 777 .next/cache +# Set correct permissions for prerender cache +RUN mkdir .next && chown nextjs:nodejs .next + +# Leverage output traces to reduce image size +COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./ +COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static + +# Copy healthcheck script +COPY --chown=nextjs:nodejs healthcheck.js . + +USER nextjs -# Expose port EXPOSE 3000 ENV PORT=3000 ENV HOSTNAME="0.0.0.0" -# Set node options to fix the IPv6 issue we discussed earlier -ENV NODE_OPTIONS="--dns-result-order=ipv4first" +HEALTHCHECK --interval=30s --timeout=10s --start-period=30s --retries=3 \ + CMD node healthcheck.js || exit 1 -# Start the app CMD ["node", "server.js"] \ No newline at end of file diff --git a/healthcheck.js b/healthcheck.js new file mode 100644 index 0000000..bcd1598 --- /dev/null +++ b/healthcheck.js @@ -0,0 +1,18 @@ +// healthcheck.js +const http = require('http'); + +const options = { + host: 'localhost', + port: process.env.PORT || 3000, + path: '/', + timeout: 5000, +}; + +const req = http.request(options, (res) => { + process.exit(res.statusCode === 200 ? 0 : 1); +}); + +req.on('error', () => process.exit(1)); +req.on('timeout', () => process.exit(1)); + +req.end(); \ No newline at end of file