Replies: 1 comment
-
@Dhruvgera if you're doing a standalone nextjs build then, it generates a full build everytime, even if you change one word, you won't see any savings using a cache mount. you can test this locally, by timing ``` time npm run build`` change one word and run again, it's the same build time even though you have the cache in the working folder however if you do want to add some cache for things like packages or .next/cache you can do the following (based on nextjs dockerfile) (i'm using yarn here) - just remember to add an id for your .next/cache as you don't want to pollute from other apps: FROM node:23-alpine AS base
ARG YARN_VERSION=4.7
RUN corepack enable && corepack prepare yarn@${YARN_VERSION} --activate
RUN yarn set version ${YARN_VERSION}
RUN apk add --no-cache libc6-compat
RUN apk add --no-cache curl
FROM base AS deps
USER node
WORKDIR /app
ENV NODE_ENV=production
COPY --chown=node:node package.json yarn.lock* package-lock.json* pnpm-lock.yaml* .yarnrc.yml* ./
USER root
# Yarn
RUN --mount=type=cache,target=/root/.cache/yarn \
if [ -f yarn.lock ]; then yarn install --immutable; fi
# npm
RUN --mount=type=cache,target=/root/.npm \
if [ -f package-lock.json ]; then npm ci; fi
# pnpm
RUN --mount=type=cache,target=/root/.pnpm-store \
if [ -f pnpm-lock.yaml ]; then pnpm install --frozen-lockfile; fi
FROM base AS builder
WORKDIR /app
COPY --from=deps --chown=node:node /app/node_modules ./node_modules
COPY --chown=node:node . .
ENV NEXT_TELEMETRY_DISABLED=1
ENV NODE_ENV=production
ARG MYAPP=APP
USER root
RUN --mount=type=cache,target=/app/.next/cache,id=${MYAPP} \
if [ -f yarn.lock ]; then yarn build; \
elif [ -f package-lock.json ]; then npm run build; \
elif [ -f pnpm-lock.yaml ]; then pnpm run build; \
else echo "Lockfile not found." && exit 1; \
fi
FROM base AS runner
WORKDIR /app
ENV NODE_ENV=production
ENV NEXT_TELEMETRY_DISABLED=1
RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 nextjs
COPY --from=builder --chown=nextjs:nodejs /app/public ./public
RUN mkdir .next
RUN chown nextjs:nodejs .next
COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static
EXPOSE 3000
ENV PORT=3000
USER nextjs
ENV HOSTNAME="0.0.0.0"
CMD ["node", "server.js"] |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
We have large NextJS repositories and compiling them every time without previous build cache takes extremely long, is there a way to re-use and update cache from previous builds to speed up the process like on other CI solutions?
Beta Was this translation helpful? Give feedback.
All reactions