Docker config

This commit is contained in:
mukesh13
2025-06-16 18:43:15 +05:30
parent 776d69654d
commit 4c8ab08a06

View File

@ -1,61 +1,32 @@
# syntax=docker.io/docker/dockerfile:1 # Use the official Node.js 20 image as the base
FROM node:20-alpine
# Base stage for Node.js # Set working directory
FROM node:20-alpine AS base
# Stage 1: Install dependencies
FROM base AS deps
# Add libc6-compat for compatibility with some Node.js native modules
RUN apk add --no-cache libc6-compat
WORKDIR /app WORKDIR /app
# Copy dependency files # Install pnpm
RUN corepack enable && corepack prepare pnpm@latest --activate
# Copy only dependency files first
COPY package.json pnpm-lock.yaml* ./ COPY package.json pnpm-lock.yaml* ./
# Install pnpm and dependencies # Install dependencies with pnpm
RUN corepack enable pnpm && pnpm install --frozen-lockfile RUN pnpm install --frozen-lockfile
# Stage 2: Build the Next.js app # Copy the rest of the application code
FROM base AS builder
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY . . COPY . .
# Create a cache directory for Next.js # Create a cache directory for Next.js
RUN mkdir -p /app/.next/cache RUN mkdir -p /app/.next/cache
# Disable telemetry during build # Disable telemetry
ENV NEXT_TELEMETRY_DISABLED=1 ENV NEXT_TELEMETRY_DISABLED=1
# Build the Next.js app # Build the Next.js application
RUN corepack enable pnpm && pnpm run build RUN pnpm build
# Stage 3: Production image
FROM base AS runner
WORKDIR /app
ENV NODE_ENV=production
ENV NEXT_TELEMETRY_DISABLED=1
# Create a non-root user
RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 nextjs
# Copy public assets
COPY --from=builder /app/public ./public
# Copy standalone output and static files
COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static
# Copy the .next/cache for runtime caching (e.g., ISR)
COPY --from=builder --chown=nextjs:nodejs /app/.next/cache ./.next/cache
USER nextjs
# Expose port 3000
EXPOSE 3000 EXPOSE 3000
ENV PORT=3000 # Start the Next.js application
ENV HOSTNAME="0.0.0.0" CMD ["pnpm", "start", "-p", "3000"]
CMD ["node", "server.js"]