Optimise multi platform build (#128)

* Use next.js standalone to optimise docker image

* Temporarily disable release branch check

* Set `PORT` env

* Use the build machine architecture to build and target platform for running

* Prep for PR

* Update node image

* Test with latest image and use ARG for image version

* Prep for PR
This commit is contained in:
Aswin V 2022-12-09 20:34:40 +05:30 committed by GitHub
parent 9eafdeee33
commit 57b8ddf17c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 24 additions and 13 deletions

View File

@ -1,8 +1,11 @@
Dockerfile
.dockerignore
node_modules
npm-debug.log
README.md
.next
.git
.github
.vscode
*Dockerfile*
.env
.env.example

View File

@ -1,39 +1,46 @@
FROM node:16.18.0-alpine3.15 AS base
ARG NODEJS_IMAGE=node:16.18.1-alpine3.16
FROM --platform=$BUILDPLATFORM $NODEJS_IMAGE AS base
FROM base AS deps
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 install
RUN npm ci
FROM base AS builder
WORKDIR /app
COPY . .
COPY --from=deps /app/node_modules ./node_modules
COPY . .
ENV NEXT_PUBLIC_GTM_ID ""
ENV NEXT_TELEMETRY_DISABLED 1
RUN npm run build && npm install --production --ignore-scripts --prefer-offline
RUN npm run build
FROM base AS runner
FROM $NODEJS_IMAGE AS runner
WORKDIR /app
ENV NODE_OPTIONS="--max-http-header-size=81920"
ENV NODE_ENV production
ENV NEXT_TELEMETRY_DISABLED 1
RUN addgroup -g 1001 -S nodejs
RUN adduser -S nextjs -u 1001
RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 nextjs
COPY --from=builder /app/public ./public
COPY --from=builder --chown=nextjs:nodejs /app/.next ./.next
COPY --from=builder /app/node_modules ./node_modules
COPY --from=builder /app/package.json ./package.json
# 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 4000
ENV NEXT_TELEMETRY_DISABLED 1
ENV PORT 4000
CMD ["npm", "start"]
CMD ["node", "server.js"]

View File

@ -1,6 +1,7 @@
/** @type {import('next').NextConfig} */
module.exports = {
reactStrictMode: true,
output: 'standalone',
webpack: (config, { isServer }) => {
if (!isServer) {
config.resolve.fallback = {