From 421ae4d66b9bd55af7b51a04bf8ce6f33285bddc Mon Sep 17 00:00:00 2001 From: dragonxp <35697471+dragonxp@users.noreply.github.com> Date: Fri, 11 Jul 2025 17:33:24 +0545 Subject: [PATCH 1/3] Added Dockerfile, .dockerignore and modified the nextconfig to output standalone --- frontend/.dockerignore | 14 ++++++++ frontend/Dockerfile | 76 ++++++++++++++++++++++++++++------------- frontend/next.config.ts | 5 +-- 3 files changed, 70 insertions(+), 25 deletions(-) create mode 100644 frontend/.dockerignore diff --git a/frontend/.dockerignore b/frontend/.dockerignore new file mode 100644 index 00000000..97456fa2 --- /dev/null +++ b/frontend/.dockerignore @@ -0,0 +1,14 @@ +Dockerfile +.dockerignore +node_modules +npm-debug.log +README.md +.next +.git + +.prettierrc +.prettierignore +*.env +*.env.* +env.* +README.md \ No newline at end of file diff --git a/frontend/Dockerfile b/frontend/Dockerfile index 56809ba5..937da751 100644 --- a/frontend/Dockerfile +++ b/frontend/Dockerfile @@ -1,34 +1,64 @@ -FROM node:20-slim +# syntax=docker.io/docker/dockerfile:1 +# ---- Base Stage ---- +FROM node:22-slim AS base + +# Install dependencies only when needed +FROM base AS deps WORKDIR /app -# Copy package files first for better layer caching -COPY package*.json ./ +# Install dependencies based on the preferred package manager +COPY package.json yarn.lock* package-lock.json* pnpm-lock.yaml* .npmrc* ./ +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 && pnpm i --frozen-lockfile; \ + else echo "Lockfile not found." && exit 1; \ + fi -# Install build dependencies for node-gyp -RUN apt-get update && apt-get install -y --no-install-recommends \ - python3 \ - make \ - g++ \ - build-essential \ - pkg-config \ - libcairo2-dev \ - libpango1.0-dev \ - libjpeg-dev \ - libgif-dev \ - librsvg2-dev \ - && rm -rf /var/lib/apt/lists/* -RUN npm install - -# Copy the frontend code +# ---- Builder Stage ---- +FROM base AS builder +WORKDIR /app +COPY --from=deps /app/node_modules ./node_modules COPY . . -ENV NEXT_PUBLIC_VERCEL_ENV production +# Next.js collects completely anonymous telemetry data about general usage. +# Learn more here: https://nextjs.org/telemetry +ENV NEXT_TELEMETRY_DISABLED=1 -RUN npm run build +RUN \ + if [ -f yarn.lock ]; then yarn run build; \ + elif [ -f package-lock.json ]; then npm run build; \ + elif [ -f pnpm-lock.yaml ]; then corepack enable pnpm && pnpm run build; \ + else echo "Lockfile not found." && exit 1; \ + fi + +# ---- Runner Stage ---- +FROM base AS runner +WORKDIR /app + +ENV NEXT_PUBLIC_VERCEL_ENV=production +ENV NODE_ENV=production +ENV NEXT_TELEMETRY_DISABLED=1 + +RUN addgroup --system --gid 1001 nodejs +RUN adduser --system --uid 1001 nextjs + +COPY --from=builder /app/public ./public + +# Automatically leverage output traces to reduce image size +# https://nextjs.org/docs/advanced-features/output-file-tracing +COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./ +COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static + +USER nextjs EXPOSE 3000 -# Default command is dev, but can be overridden in docker-compose -CMD ["npm", "start"] \ No newline at end of file +ENV PORT=3000 + +# server.js is created by next build from the standalone output +# https://nextjs.org/docs/pages/api-reference/config/next-config-js/output +ENV HOSTNAME="0.0.0.0" +CMD ["node", "server.js"] \ No newline at end of file diff --git a/frontend/next.config.ts b/frontend/next.config.ts index 77c86ad1..9e13e5f6 100644 --- a/frontend/next.config.ts +++ b/frontend/next.config.ts @@ -1,6 +1,7 @@ import type { NextConfig } from 'next'; -const nextConfig: NextConfig = { -}; +const nextConfig = (): NextConfig => ({ + output: 'standalone', +}); export default nextConfig; From 9752a9ab0c47c0713ecb985bd36fa7892937c584 Mon Sep 17 00:00:00 2001 From: dragonxp <35697471+dragonxp@users.noreply.github.com> Date: Sat, 12 Jul 2025 18:40:49 +0545 Subject: [PATCH 2/3] Updated next config to support dynamic deployment --- frontend/Dockerfile | 1 + frontend/next.config.ts | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/frontend/Dockerfile b/frontend/Dockerfile index 937da751..69cea002 100644 --- a/frontend/Dockerfile +++ b/frontend/Dockerfile @@ -26,6 +26,7 @@ COPY . . # Next.js collects completely anonymous telemetry data about general usage. # Learn more here: https://nextjs.org/telemetry ENV NEXT_TELEMETRY_DISABLED=1 +ENV NEXT_OUTPUT=standalone RUN \ if [ -f yarn.lock ]; then yarn run build; \ diff --git a/frontend/next.config.ts b/frontend/next.config.ts index 9e13e5f6..efd187d5 100644 --- a/frontend/next.config.ts +++ b/frontend/next.config.ts @@ -1,7 +1,7 @@ import type { NextConfig } from 'next'; const nextConfig = (): NextConfig => ({ - output: 'standalone', + output: (process.env.NEXT_OUTPUT as 'standalone') || undefined, }); export default nextConfig; From b88672be2a3f73d74519f0d1f381e5f6c2225c04 Mon Sep 17 00:00:00 2001 From: dragonxp <35697471+dragonxp@users.noreply.github.com> Date: Sat, 12 Jul 2025 21:45:42 +0545 Subject: [PATCH 3/3] Removed env and run command from compose file --- docker-compose.yaml | 3 --- frontend/.dockerignore | 1 - 2 files changed, 4 deletions(-) diff --git a/docker-compose.yaml b/docker-compose.yaml index 0c9f7f60..094db576 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -88,9 +88,6 @@ services: - "3000:3000" volumes: - ./frontend/.env.local:/app/.env.local:ro - environment: - - NODE_ENV=production - command: ["npm", "run", "start"] depends_on: - backend diff --git a/frontend/.dockerignore b/frontend/.dockerignore index 97456fa2..3d68d349 100644 --- a/frontend/.dockerignore +++ b/frontend/.dockerignore @@ -9,6 +9,5 @@ README.md .prettierrc .prettierignore *.env -*.env.* env.* README.md \ No newline at end of file