From 521b3ded9c5803c4f915610033d4c4e728103108 Mon Sep 17 00:00:00 2001 From: Sergio Garcia Date: Wed, 13 Nov 2024 10:15:33 -0500 Subject: [PATCH] fix(Dockerfile): ensure correct deployment (#92) * fix(Dockerfile): ensure correct deployment * chore(dockerfile): Add NEXT_TELEMETRY_DISABLED=1 --------- Co-authored-by: Pepe Fagoaga --- .env.template | 1 + Dockerfile | 64 +++++++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 56 insertions(+), 9 deletions(-) diff --git a/.env.template b/.env.template index 0ac689045f..ba212ad68a 100644 --- a/.env.template +++ b/.env.template @@ -1,5 +1,6 @@ SITE_URL=http://localhost:3000 API_BASE_URL=http://localhost:8080/api/v1 +AUTH_TRUST_HOST=true # openssl rand -base64 32 AUTH_SECRET=your-secret-key diff --git a/Dockerfile b/Dockerfile index ee47649043..a8c698c014 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,24 +1,70 @@ -# Base image for Node FROM node:20-alpine AS base LABEL maintainer="https://github.com/prowler-cloud" +# 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. +#hadolint ignore=DL3018 +RUN apk add --no-cache libc6-compat WORKDIR /app -# Install dependencies only when needed -RUN apk add --no-cache libc6-compat - -# Copy package.json and lock files to install dependencies +# Install dependencies based on the preferred package manager COPY package.json yarn.lock* package-lock.json* pnpm-lock.yaml* ./ -RUN if [ -f package-lock.json ]; then npm install; else echo "Lockfile not found." && exit 1; fi +RUN \ + if [ -f package-lock.json ]; then npm install; \ + else echo "Lockfile not found." && exit 1; \ + fi -# Copy the rest of the application code + +# Rebuild the source code only when needed +FROM base AS builder +WORKDIR /app +COPY --from=deps /app/node_modules ./node_modules COPY . . +# Next.js collects completely anonymous telemetry data about general usage. +# Learn more here: https://nextjs.org/telemetry +# Uncomment the following line in case you want to disable telemetry during the build. +ENV NEXT_TELEMETRY_DISABLED=1 + +RUN \ + if [ -f package-lock.json ]; then npm run build; \ + else echo "Lockfile not found." && exit 1; \ + fi + # Development stage FROM base AS dev +WORKDIR /app + +# Set up environment for development +ENV NODE_ENV=development +ENV NEXT_TELEMETRY_DISABLED=1 +COPY --from=builder /app /app + +# Run development server with hot-reloading CMD ["npm", "run", "dev"] # Production stage FROM base AS prod -RUN npm run build -CMD ["npm", "start"] +WORKDIR /app + +# Set up environment for production +ENV NODE_ENV=production +ENV NEXT_TELEMETRY_DISABLED=1 + +RUN addgroup --system --gid 1001 nodejs &&\ +adduser --system --uid 1001 nextjs + +COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./ +COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static +COPY --from=builder --chown=nextjs:nodejs /app/public ./public + +USER nextjs + +EXPOSE 3000 + +ENV PORT=3000 +ENV HOSTNAME="0.0.0.0" +# server.js is created by next build from the standalone output +# https://nextjs.org/docs/pages/api-reference/next-config-js/output +CMD ["node", "server.js"]