diff --git a/Dockerfile b/Dockerfile index f668358..22780aa 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,19 +1,50 @@ -FROM node:21-alpine as builder -# Add a work directory +FROM node:18-alpine AS base + +# 1. 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 ./next-app/package.json ./ +COPY ./next-app/package-lock.json ./ +RUN \ + if [ -f package-lock.json ]; then npm ci; \ + else echo "Lockfile not found." && exit 1; \ + fi + + +# 2. Rebuild the source code only when needed +FROM base AS builder WORKDIR /app -RUN chown -R node:node /app -USER node -# Cache and Install dependencies -COPY ./pmp-frontend-app/package.json . -COPY ./pmp-frontend-app/package-lock.json . -RUN npm ci -# Copy app files -COPY ./pmp-frontend-app/ . +COPY --from=deps /app/node_modules ./node_modules +COPY ./next-app/ . +# This will do the trick, use the corresponding env file for each environment. RUN npm run build -FROM nginxinc/nginx-unprivileged:stable-alpine -USER 101 -COPY ./conf/nginx.conf /etc/nginx/conf.d/default.conf -COPY --from=builder /app/dist /usr/share/nginx/html -EXPOSE 8080 -CMD ["nginx", "-g", "daemon off;"] \ No newline at end of file +# 3. Production image, copy all the files and run next +FROM base AS runner +WORKDIR /app + +ENV NODE_ENV=production + +RUN addgroup -g 1001 -S nodejs +RUN adduser -S nextjs -u 1001 + +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 + +ENV PORT=3000 + +CMD HOSTNAME="0.0.0.0" node server.js \ No newline at end of file diff --git a/_nextjs_Dockerfile b/_nextjs_Dockerfile deleted file mode 100644 index 22780aa..0000000 --- a/_nextjs_Dockerfile +++ /dev/null @@ -1,50 +0,0 @@ -FROM node:18-alpine AS base - -# 1. 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 ./next-app/package.json ./ -COPY ./next-app/package-lock.json ./ -RUN \ - if [ -f package-lock.json ]; then npm ci; \ - else echo "Lockfile not found." && exit 1; \ - fi - - -# 2. Rebuild the source code only when needed -FROM base AS builder -WORKDIR /app -COPY --from=deps /app/node_modules ./node_modules -COPY ./next-app/ . -# This will do the trick, use the corresponding env file for each environment. -RUN npm run build - -# 3. Production image, copy all the files and run next -FROM base AS runner -WORKDIR /app - -ENV NODE_ENV=production - -RUN addgroup -g 1001 -S nodejs -RUN adduser -S nextjs -u 1001 - -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 - -ENV PORT=3000 - -CMD HOSTNAME="0.0.0.0" node server.js \ No newline at end of file diff --git a/_vite_Dockerfile b/_vite_Dockerfile new file mode 100644 index 0000000..f668358 --- /dev/null +++ b/_vite_Dockerfile @@ -0,0 +1,19 @@ +FROM node:21-alpine as builder +# Add a work directory +WORKDIR /app +RUN chown -R node:node /app +USER node +# Cache and Install dependencies +COPY ./pmp-frontend-app/package.json . +COPY ./pmp-frontend-app/package-lock.json . +RUN npm ci +# Copy app files +COPY ./pmp-frontend-app/ . +RUN npm run build + +FROM nginxinc/nginx-unprivileged:stable-alpine +USER 101 +COPY ./conf/nginx.conf /etc/nginx/conf.d/default.conf +COPY --from=builder /app/dist /usr/share/nginx/html +EXPOSE 8080 +CMD ["nginx", "-g", "daemon off;"] \ No newline at end of file