-
Notifications
You must be signed in to change notification settings - Fork 7.2k
Upgrade Next.js & DB dependencies; optimize Docker image size #4106
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,66 +1,60 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||
| ARG NODE_IMAGE_VERSION="22-alpine" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| # syntax=docker/dockerfile:1.7 | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| # Install dependencies only when needed | ||||||||||||||||||||||||||||||||||||||||||||||||||
| FROM node:${NODE_IMAGE_VERSION} 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 pnpm-lock.yaml ./ | ||||||||||||||||||||||||||||||||||||||||||||||||||
| RUN npm install -g pnpm | ||||||||||||||||||||||||||||||||||||||||||||||||||
| RUN pnpm install --frozen-lockfile | ||||||||||||||||||||||||||||||||||||||||||||||||||
| ARG NODE_VERSION=24-alpine | ||||||||||||||||||||||||||||||||||||||||||||||||||
| ARG PNPM_VERSION=10.32.1 | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| # Rebuild the source code only when needed | ||||||||||||||||||||||||||||||||||||||||||||||||||
| FROM node:${NODE_IMAGE_VERSION} AS builder | ||||||||||||||||||||||||||||||||||||||||||||||||||
| FROM node:${NODE_VERSION} AS base | ||||||||||||||||||||||||||||||||||||||||||||||||||
| WORKDIR /app | ||||||||||||||||||||||||||||||||||||||||||||||||||
| COPY --from=deps /app/node_modules ./node_modules | ||||||||||||||||||||||||||||||||||||||||||||||||||
| COPY . . | ||||||||||||||||||||||||||||||||||||||||||||||||||
| COPY docker/middleware.ts ./src | ||||||||||||||||||||||||||||||||||||||||||||||||||
| ENV NEXT_TELEMETRY_DISABLED=1 | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| ARG BASE_PATH | ||||||||||||||||||||||||||||||||||||||||||||||||||
| FROM base AS deps | ||||||||||||||||||||||||||||||||||||||||||||||||||
| RUN corepack enable && corepack prepare pnpm@${PNPM_VERSION} --activate | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| ENV BASE_PATH=$BASE_PATH | ||||||||||||||||||||||||||||||||||||||||||||||||||
| ENV NEXT_TELEMETRY_DISABLED=1 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| ENV DATABASE_URL="postgresql://user:pass@localhost:5432/dummy" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| COPY package.json ./ | ||||||||||||||||||||||||||||||||||||||||||||||||||
| # 如果你们有这些文件,也要一起 copy,避免依赖层失真 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| # COPY .npmrc pnpm-workspace.yaml ./ | ||||||||||||||||||||||||||||||||||||||||||||||||||
| # COPY patches ./patches | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| RUN npm run build-docker | ||||||||||||||||||||||||||||||||||||||||||||||||||
| RUN --mount=type=cache,id=pnpm,target=/pnpm/store \ | ||||||||||||||||||||||||||||||||||||||||||||||||||
| pnpm config set store-dir /pnpm/store && \ | ||||||||||||||||||||||||||||||||||||||||||||||||||
| pnpm install | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| # Production image, copy all the files and run next | ||||||||||||||||||||||||||||||||||||||||||||||||||
| FROM node:${NODE_IMAGE_VERSION} AS runner | ||||||||||||||||||||||||||||||||||||||||||||||||||
| WORKDIR /app | ||||||||||||||||||||||||||||||||||||||||||||||||||
| FROM base AS builder | ||||||||||||||||||||||||||||||||||||||||||||||||||
| RUN corepack enable && corepack prepare pnpm@${PNPM_VERSION} --activate | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+11
to
+23
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Both the
Suggested change
Then remove the |
||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| ARG PRISMA_VERSION="6.19.0" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| ARG NODE_OPTIONS | ||||||||||||||||||||||||||||||||||||||||||||||||||
| COPY --from=deps /app/node_modules ./node_modules | ||||||||||||||||||||||||||||||||||||||||||||||||||
| COPY . . | ||||||||||||||||||||||||||||||||||||||||||||||||||
| COPY docker/middleware.ts ./src/middleware.ts | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| ARG BASE_PATH | ||||||||||||||||||||||||||||||||||||||||||||||||||
| ENV NODE_ENV=production | ||||||||||||||||||||||||||||||||||||||||||||||||||
| ENV NEXT_TELEMETRY_DISABLED=1 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| ENV NODE_OPTIONS=$NODE_OPTIONS | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| RUN addgroup --system --gid 1001 nodejs | ||||||||||||||||||||||||||||||||||||||||||||||||||
| RUN adduser --system --uid 1001 nextjs | ||||||||||||||||||||||||||||||||||||||||||||||||||
| RUN set -x \ | ||||||||||||||||||||||||||||||||||||||||||||||||||
| && apk add --no-cache curl \ | ||||||||||||||||||||||||||||||||||||||||||||||||||
| && npm install -g pnpm | ||||||||||||||||||||||||||||||||||||||||||||||||||
| RUN --mount=type=cache,id=pnpm,target=/pnpm/store \ | ||||||||||||||||||||||||||||||||||||||||||||||||||
| BASE_PATH="${BASE_PATH}" \ | ||||||||||||||||||||||||||||||||||||||||||||||||||
| DATABASE_URL="postgresql://user:pass@localhost:5432/dummy" \ | ||||||||||||||||||||||||||||||||||||||||||||||||||
| pnpm run build-docker | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| # Script dependencies | ||||||||||||||||||||||||||||||||||||||||||||||||||
| RUN pnpm --allow-build='@prisma/engines' add npm-run-all dotenv chalk semver \ | ||||||||||||||||||||||||||||||||||||||||||||||||||
| prisma@${PRISMA_VERSION} \ | ||||||||||||||||||||||||||||||||||||||||||||||||||
| @prisma/adapter-pg@${PRISMA_VERSION} | ||||||||||||||||||||||||||||||||||||||||||||||||||
| FROM base AS runner | ||||||||||||||||||||||||||||||||||||||||||||||||||
| ENV NODE_ENV=production | ||||||||||||||||||||||||||||||||||||||||||||||||||
| ENV HOSTNAME=0.0.0.0 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| ENV PORT=3000 | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| COPY --from=builder --chown=nextjs:nodejs /app/public ./public | ||||||||||||||||||||||||||||||||||||||||||||||||||
| COPY --from=builder /app/prisma ./prisma | ||||||||||||||||||||||||||||||||||||||||||||||||||
| COPY --from=builder /app/scripts ./scripts | ||||||||||||||||||||||||||||||||||||||||||||||||||
| COPY --from=builder /app/generated ./generated | ||||||||||||||||||||||||||||||||||||||||||||||||||
| # 用官方 node 镜像自带的 node 用户即可,少一层自建用户逻辑 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| COPY --from=builder --chown=node:node /app/public ./public | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| # 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 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| # Next 运行时缓存/ISR 目录 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| RUN mkdir .next && chown node:node .next | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| USER nextjs | ||||||||||||||||||||||||||||||||||||||||||||||||||
| # standalone 最小运行集 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| COPY --from=builder --chown=node:node /app/.next/standalone ./ | ||||||||||||||||||||||||||||||||||||||||||||||||||
| COPY --from=builder --chown=node:node /app/.next/static ./.next/static | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| EXPOSE 3000 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| # 只有当运行时真的需要这些文件时才复制 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| # COPY --from=builder --chown=node:node /app/prisma ./prisma | ||||||||||||||||||||||||||||||||||||||||||||||||||
| # COPY --from=builder --chown=node:node /app/generated ./generated | ||||||||||||||||||||||||||||||||||||||||||||||||||
| # COPY --from=builder --chown=node:node /app/scripts ./scripts | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+14
to
+55
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Several comments in this Dockerfile are written in Chinese (e.g., lines 14, 42, 45, 52). The rest of the repository and all existing Dockerfile comments are in English. Please translate these to English so all contributors can understand the rationale without a translation tool. For example, line 14 ( Line 42 ( |
||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| ENV HOSTNAME=0.0.0.0 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| ENV PORT=3000 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| USER node | ||||||||||||||||||||||||||||||||||||||||||||||||||
| EXPOSE 3000 | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| CMD ["pnpm", "start-docker"] | ||||||||||||||||||||||||||||||||||||||||||||||||||
| CMD ["node", "server.js"] | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The original
By replacing the command with the bare The
Suggested change
Alternatively, restore the previous CMD and un-comment the COPY --from=builder --chown=node:node /app/prisma ./prisma
COPY --from=builder --chown=node:node /app/generated ./generated
COPY --from=builder --chown=node:node /app/scripts ./scripts
CMD ["pnpm", "start-docker"](and add |
||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
pnpm-lock.yamlis not copied — non-deterministic buildsThe deps stage copies only
package.jsonand runspnpm installwithout--frozen-lockfile. The original Dockerfile explicitly copiedpnpm-lock.yamlto pin exact dependency versions. Without the lockfile:^16range fornextand the loose^6ranges for Prisma packages mean any patch/minor could be pulled in at any build