finished docker build & deploy

This commit is contained in:
2024-01-10 13:27:25 +01:00
parent 77fefe6868
commit 3e56ea3ae6
5 changed files with 100 additions and 27 deletions

View File

@@ -1,32 +1,58 @@
# Stage 1: Build the Next.js project
FROM node:18 AS builder
# This file is inspired by https://github.com/vercel/next.js/blob/canary/examples/with-docker/Dockerfile
FROM node:18-alpine AS base
#-----------------------------------------
# STAGE 1: Build the Next.js project
#-----------------------------------------
FROM base AS builder
# 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
ENV WORKDIR=/app
WORKDIR /app
# copy all the soruce code
COPY . .
# package.json and package-lock.json
COPY ./package.json ./package-lock.json ./
# installing dependencies
RUN npm i && npm cache clean --force
# copy all the soruce code
COPY . .
# building app
RUN npm run build
# remove dev dependencies
RUN rm -rf node_modules
# installing production dependencies
RUN npm i --verbose --only=production && npm cache clean --force
# Stage 2: Run the Next.js server
FROM gcr.io/distroless/nodejs:18 as prod-image
#-----------------------------------------
# STAGE 3: Run the Next.js server
#-----------------------------------------
FROM base as production
WORKDIR /app
COPY --from=builder /app/package.json /app/package-lock.json ./
ENV NODE_ENV production
COPY --from=builder /app/.next ./.next
COPY ./public /app/public
RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 nextjs
CMD ["npm", "start"]
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
# 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
# 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"]