fix: reduce docker image size (#557)

* feat: enable standalone output

* feat: update docker file

* fix: alias as base and pin node version

* fix: copy npm folder

* fix: install sharp

* build: update docker ignore

* fix: remove sharp from docker file

* fix: remove asteriks

* feat: add max http header size env

* fix: remove copy npm folder
This commit is contained in:
Nafees Nazik 2022-10-08 22:53:27 +05:30 committed by GitHub
parent 12994b8eea
commit 8ccdcdcb86
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 44 additions and 17 deletions

View File

@ -1,6 +1,13 @@
node_modules
Dockerfile
.dockerignore
**/node_modules
npm-debug.log
README.md
.next
.git
.github
_dev
.vscode
swagger
uffizzi
.husky

View File

@ -5,41 +5,60 @@ 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
COPY package.json package-lock.json ./
# Install dependencies based on the preferred package manager
COPY package.json package-lock.json ./
COPY npm npm
RUN npm install --legacy-peer-deps
RUN npm ci
# Rebuild the source code only when needed
FROM base AS builder
WORKDIR /app
COPY . .
COPY --from=deps /app/npm ./npm
COPY --from=deps /app/node_modules ./node_modules
RUN npm run build && npm install --legacy-peer-deps --production --ignore-scripts --prefer-offline
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 npm run build
# Production image, copy all the files and run next
FROM base AS runner
WORKDIR /app
ENV NODE_OPTIONS="--max-http-header-size=81920"
ENV NODE_ENV production
RUN addgroup -g 1001 -S nodejs
RUN adduser -S nextjs -u 1001
ENV NODE_ENV production
# Uncomment the following line in case you want to disable telemetry during runtime.
ENV NEXT_TELEMETRY_DISABLED 1
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/npm ./npm
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 5225
# 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.
ENV NEXT_TELEMETRY_DISABLED 1
ENV PORT 5225
CMD ["node", "server.js"]
CMD ["npm", "start"]

View File

@ -2,6 +2,7 @@
module.exports = {
reactStrictMode: true,
output: 'standalone',
webpack: (config, { webpack, isServer }) => {
if (isServer) {
// Module not found