背景

最近在部署OfferNow的时候,每次推送到线上之后,repo的pnpm-lock就变了。

纳闷啊,为什么呢?

直到我发现了这个:

pnpm lockfile版本不一致 - 图1

原来每次我推送的时候lockfileVersion都是9.0.

但是一旦从线上拉下来,就变成了6.0. 关键在于我在dockerfile里没有指定pnpm的版本

解决

以下是如何在 Dockerfile 中指定 pnpm 版本的步骤:

1. 使用 corepack 启用 pnpm 并指定版本

corepack 是 Node.js 16.9.0 及以上版本自带的一个工具,用于管理包管理器的版本。你可以在 deps 阶段使用 corepack 来启用 pnpm 并指定特定版本。

2. 修改 Dockerfile

以下是修改后的 Dockerfile

  1. FROM node:18-alpine AS base
  2. # Install dependencies only when needed
  3. FROM base AS deps
  4. # Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed.
  5. RUN apk add --no-cache libc6-compat
  6. WORKDIR /app
  7. # Install dependencies based on the preferred package manager
  8. COPY package.json yarn.lock* package-lock.json* pnpm-lock.yaml* ./
  9. # Enable corepack and install specific version of pnpm
  10. RUN corepack enable && corepack prepare pnpm@<version> --activate
  11. RUN \
  12. if [ -f yarn.lock ]; then yarn --frozen-lockfile; \
  13. elif [ -f package-lock.json ]; then npm ci; \
  14. elif [ -f pnpm-lock.yaml ]; then pnpm i --frozen-lockfile; \
  15. else echo "Lockfile not found." && exit 1; \
  16. fi
  17. # Rebuild the source code only when needed
  18. FROM base AS builder
  19. WORKDIR /app
  20. COPY --from=deps /app/node_modules ./node_modules
  21. COPY . .
  22. # Next.js collects completely anonymous telemetry data about general usage.
  23. # Learn more here: https://nextjs.org/telemetry
  24. # Uncomment the following line in case you want to disable telemetry during the build.
  25. ENV NEXT_TELEMETRY_DISABLED 1
  26. RUN \
  27. if [ -f yarn.lock ]; then yarn run build; \
  28. elif [ -f package-lock.json ]; then npm run build; \
  29. elif [ -f pnpm-lock.yaml ]; then pnpm run build; \
  30. else echo "Lockfile not found." && exit 1; \
  31. fi
  32. # runner阶段创建最小镜像以运行next应用
  33. FROM base AS runner
  34. WORKDIR /app
  35. ENV NODE_ENV production
  36. # Uncomment the following line in case you want to disable telemetry during runtime.
  37. ENV NEXT_TELEMETRY_DISABLED 1
  38. # 创建用户和组
  39. RUN addgroup --system --gid 1001 nodejs
  40. RUN adduser --system --uid 1001 nextjs
  41. COPY --from=builder /app/public ./public
  42. # Set the correct permission for prerender cache
  43. RUN mkdir .next
  44. RUN chown nextjs:nodejs .next
  45. # Automatically leverage output traces to reduce image size
  46. # https://nextjs.org/docs/advanced-features/output-file-tracing
  47. COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
  48. COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static
  49. USER nextjs
  50. EXPOSE 3020
  51. ENV PORT 3020
  52. # server.js is created by next build from the standalone output
  53. # https://nextjs.org/docs/pages/api-reference/next-config-js/output
  54. # 启动服务
  55. CMD ["node", "server.js"]

3. 替换 <version>

corepack prepare pnpm@<version> --activate 这一行中,将 <version> 替换为你想要使用的 pnpm 版本号,例如 7.0.0

4. 构建和运行 Docker 镜像

保存修改后的 Dockerfile,然后构建和运行 Docker 镜像:

  1. docker build -t my-nextjs-app .
  2. docker run -p 3020:3020 my-nextjs-app

通过这种方式,可以在 Dockerfile 中指定 pnpm 的版本,确保在不同环境中使用一致的 pnpm 版本。