Optimize frontend for production using multi-stage standalone build

This commit is contained in:
2026-07-27 13:26:31 +05:30
parent f5049ef965
commit c214ca41b6
3 changed files with 72 additions and 7 deletions

53
Dockerfile Normal file
View File

@@ -0,0 +1,53 @@
FROM node:20-alpine AS base
# Install dependencies only when needed
FROM base AS deps
# Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed.
RUN apk add --no-cache libc6-compat
WORKDIR /app
# Install dependencies based on the preferred package manager
COPY package.json package-lock.json* ./
RUN npm ci
# Rebuild the source code only when needed
FROM base AS builder
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY . .
# Set environment variables for build
ENV NEXT_TELEMETRY_DISABLED=1
RUN npm run build
# Production image, copy all the files and run next
FROM base AS runner
WORKDIR /app
ENV NODE_ENV=production
ENV NEXT_TELEMETRY_DISABLED=1
RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 nextjs
# Automatically leverage output traces to reduce image size
# https://nextjs.org/docs/advanced-features/output-file-tracing
COPY --from=builder /app/public ./public
# Set the correct permission for prerender cache
RUN mkdir .next
RUN chown nextjs:nodejs .next
# Automatically 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
USER nextjs
EXPOSE 3000
ENV PORT=3000
ENV HOSTNAME="0.0.0.0"
CMD ["node", "server.js"]

View File

@@ -20,6 +20,20 @@ services:
timeout: 5s
retries: 5
pgbouncer:
image: edoburu/pgbouncer
container_name: pragativeer_pgbouncer
environment:
- DATABASE_URL=postgres://pragativeer_admin:local_secure_password_2026@postgres:5432/pragativeer_prod
- POOL_MODE=transaction
- MAX_CLIENT_CONN=1000
- DEFAULT_POOL_SIZE=50
depends_on:
postgres:
condition: service_healthy
networks:
- pragativeer_network
redis:
image: redis:7-alpine
container_name: pragativeer_cache
@@ -41,7 +55,7 @@ services:
ports:
- "8000:8000"
environment:
- DATABASE_URL=postgresql://pragativeer_admin:local_secure_password_2026@postgres:5432/pragativeer_prod
- DATABASE_URL=postgresql://pragativeer_admin:local_secure_password_2026@pgbouncer:5432/pragativeer_prod
- REDIS_URL=redis://redis:6379/0
- ENV=development
- MINIO_ENDPOINT=minio:9000
@@ -65,7 +79,7 @@ services:
volumes:
- ./backend:/app
environment:
- DATABASE_URL=postgresql://pragativeer_admin:local_secure_password_2026@postgres:5432/pragativeer_prod
- DATABASE_URL=postgresql://pragativeer_admin:local_secure_password_2026@pgbouncer:5432/pragativeer_prod
- REDIS_URL=redis://redis:6379/0
- MINIO_ENDPOINT=minio:9000
- MINIO_ACCESS_KEY=minio_admin
@@ -88,7 +102,7 @@ services:
volumes:
- ./backend:/app
environment:
- DATABASE_URL=postgresql://pragativeer_admin:local_secure_password_2026@postgres:5432/pragativeer_prod
- DATABASE_URL=postgresql://pragativeer_admin:local_secure_password_2026@pgbouncer:5432/pragativeer_prod
- REDIS_URL=redis://redis:6379/0
env_file:
- ./backend/.env
@@ -101,13 +115,10 @@ services:
frontend:
build:
context: .
dockerfile: Dockerfile.dev
dockerfile: Dockerfile
container_name: pragativeer_frontend
ports:
- "3000:3000"
volumes:
- .:/app
- /app/node_modules
environment:
- NEXT_PUBLIC_API_URL=http://localhost:8000
depends_on:

View File

@@ -1,6 +1,7 @@
import type { NextConfig } from "next";
const nextConfig: NextConfig = {
output: "standalone",
async rewrites() {
return [
{