Skip to content

Instantly share code, notes, and snippets.

@vanxh
Last active April 16, 2024 22:18
Show Gist options
  • Save vanxh/0c3a62cc6bd6b8aa143c2e278d9e9dfa to your computer and use it in GitHub Desktop.
Save vanxh/0c3a62cc6bd6b8aa143c2e278d9e9dfa to your computer and use it in GitHub Desktop.
Dockerfile to deploy NextJS using yarn v3 (berry) PnP.
Dockerfile
.dockerignore
node_modules
npm-debug.log
README.md
# .next
.git
.DS_Store
.yarn/*
!.yarn/cache
!.yarn/patches
!.yarn/plugins
!.yarn/releases
!.yarn/sdks
!.yarn/versions
# Install dependencies only when needed
FROM node:alpine 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 .yarn ./.yarn
COPY .pnp.cjs .yarnrc.yml package.json yarn.lock* ./
RUN yarn install --immutable
FROM node:alpine AS runner
WORKDIR /app
COPY --from=deps /app/.yarn ./.yarn
COPY --from=deps /app/.pnp.cjs ./pnp.cjs
COPY . .
ENV NODE_ENV production
ENV NEXT_TELEMETRY_DISABLED 1
RUN yarn build
RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 nextjs
USER nextjs
EXPOSE 3000
ENV PORT 3000
CMD ["yarn","node", ".next/standalone/server.js"]
@JimmyLv
Copy link

JimmyLv commented Apr 13, 2023

Hey, this is awesome! How to generate the .pnp.cjs?
Is it the same thing as the .yarn/releases/yarn-3.5.0.cjs generated by yarn 3.5?

@francoisjacques
Copy link

@vanxh on line 15, you copy to ./pnp.cjs, renaming it in the process. Can you explain why this is required?

@molavec
Copy link

molavec commented Dec 7, 2023

In my case I'm working with yarn 4.0.2 because it is needed by Storybook.

The key was copy .yarn folder:

COPY --from=deps /app/.yarn ./.yarn

and copy yarnrc.yml with nodeLinker: node-modules option

#.yarnrc.yml 
nodeLinker: node-modules
COPY  .yarnrc.yml ./

Thanks a lot to share it!

Here is my Dockerfile for Yarn 4.0.2
https://gist.github.com/molavec/89a184c8f257e2a025ebb1d63691c288

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment