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 node_modules
npm-debug.log npm-debug.log
README.md
.next
.git .git
.github .github
.vscode .vscode
*Dockerfile*
.env .env
.env.example .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 FROM base AS deps
RUN apk add --no-cache libc6-compat RUN apk add --no-cache libc6-compat
WORKDIR /app WORKDIR /app
# Install dependencies based on the preferred package manager
COPY package.json package-lock.json ./ COPY package.json package-lock.json ./
RUN npm install RUN npm ci
FROM base AS builder FROM base AS builder
WORKDIR /app WORKDIR /app
COPY . .
COPY --from=deps /app/node_modules ./node_modules COPY --from=deps /app/node_modules ./node_modules
COPY . .
ENV NEXT_PUBLIC_GTM_ID "" ENV NEXT_PUBLIC_GTM_ID ""
ENV NEXT_TELEMETRY_DISABLED 1 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 WORKDIR /app
ENV NODE_OPTIONS="--max-http-header-size=81920" ENV NODE_OPTIONS="--max-http-header-size=81920"
ENV NODE_ENV production 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 /app/public ./public
COPY --from=builder --chown=nextjs:nodejs /app/.next ./.next
COPY --from=builder /app/node_modules ./node_modules # Automatically leverage output traces to reduce image size
COPY --from=builder /app/package.json ./package.json # 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 USER nextjs
EXPOSE 4000 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} */ /** @type {import('next').NextConfig} */
module.exports = { module.exports = {
reactStrictMode: true, reactStrictMode: true,
output: 'standalone',
webpack: (config, { isServer }) => { webpack: (config, { isServer }) => {
if (!isServer) { if (!isServer) {
config.resolve.fallback = { config.resolve.fallback = {