-
i'm migrating an old project from 1.x to 3.x. there are like thousands of articles about using docker with yarn 1 (or npm) to speed up the builds later: first copy the FROM ...
# user, workdir, etc
COPY package.json yarn.lock ./
RUN ["yarn", "install"]
COPY . ./
CMD ["yarn", "run"] with yarn 2 (and now 3) we have way more yarn-related files:
and i haven't found anything what should and should't be copied into the image. so i'm wondering what's the best way to copy those. afaik neither FROM ...
# user, workdir, etc
COPY .yarn/ ./.yarn/
COPY .pnp.cjs .yarnrc.yml package.json yarn.lock ./
RUN ["yarn", "install"]
COPY . ./
CMD ["yarn", "run"] but can i just copy the whole FROM ...
# user, workdir, etc
COPY .yarn/cache/ ./.yarn/cache/
COPY .yarn/patches/ ./.yarn/patches/
COPY .yarn/plugins/ ./.yarn/plugins/
COPY .yarn/releases/ ./.yarn/releases/
COPY .yarn/sdks/ ./.yarn/sdks/
COPY .yarn/versions/ ./.yarn/versions/
COPY .pnp.cjs .yarnrc.yml package.json yarn.lock ./
RUN ["yarn", "install"]
COPY . ./
CMD ["yarn", "run"] but that's a lot of layers so it's probably better to add them to the
FROM ...
# user, workdir, etc
COPY .yarn/ ./.yarn/
COPY .pnp.cjs .yarnrc.yml package.json yarn.lock ./
RUN ["yarn", "install"]
COPY . ./
CMD ["yarn", "run"]
what do i miss? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 3 replies
-
I'm using @Dcard's yarn-plugin-docker-build to get optimized docker image in monorepo It performs dependency analysis and integrates elegantly with the I got an image that was reduced by 60% compared to the previous image. My Dockerfile looks like: FROM node:14-alpine as builder
WORKDIR /usr/src/app
COPY manifests ./
RUN yarn install --immutable
FROM node:14-alpine
# Some build args here...
ARG ...=...
WORKDIR /usr/src/app
COPY --from=builder /usr/src/app/.pnp.js ./
COPY --from=builder /usr/src/app/.yarn/cache/ .yarn/cache/
COPY --from=builder /usr/src/app/.yarn/patches/ .yarn/patches/
COPY --from=builder /usr/src/app/.yarn/unplugged/ .yarn/unplugged/
COPY packs/packages/ ./packages/
COPY packs/platform-server/dist ./platform-server/
# Some runtime env here...
ENV NODE_ENV="production"
ENV PORT="3000"
EXPOSE 3000
CMD ["node", "-r", "./.pnp.js", "./platform-server/index.js"] |
Beta Was this translation helpful? Give feedback.
-
Example zero-installs Dockerfile that I use with a Next.js application # Install dependencies and rebuild the source code only when neededonly
FROM node:16.6.1-alpine3.13 AS builder
ENV NODE_ENV production
# 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 . .
RUN cd /app && echo 'YARN VERSION IN BUILDER: ' && yarn --version
# Note yarn rebuild - this is to let yarn rebuild binaries
RUN yarn rebuild && yarn build
# Production image, copy all the files and run next
FROM node:alpine AS runner
RUN addgroup -g 1001 -S nodejs
RUN adduser -S nextjs -u 1001
ENV NODE_ENV production
WORKDIR /app
# You only need to copy next.config.js if you are NOT using the default configuration
COPY --from=builder /app/next.config.js ./
COPY --from=builder /app/public ./public
COPY --from=builder /app/.next ./.next
COPY --from=builder /app/.yarn ./.yarn
COPY --from=builder /app/yarn.lock ./yarn.lock
COPY --from=builder /app/.yarnrc.yml ./.yarnrc.yml
COPY --from=builder /app/.pnp.cjs ./.pnp.cjs
COPY --from=builder /app/package.json ./package.json
# The step below is from the Next.js Dockerfile example, but we don't need it because we use Yarn's Zero-installs.
# COPY --from=builder /app/node_modules ./node_modules
# Note yarn rebuild again - this is to let yarn rebuild binaries in the "runner" stage of the Dockerfile
# We also have to remove unplugged, so that rebuilding happens and replaces the old binaries
RUN rm -rf /app/.yarn/unplugged && yarn rebuild
RUN chown -R nextjs:nodejs /app/.next
RUN echo "YARN VERSION IN RUNNER: " && yarn --version
USER nextjs
EXPOSE 3000
# 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
CMD ["yarn", "start"]
.yarn/*
!.yarn/cache
!.yarn/patches
!.yarn/plugins
!.yarn/releases
!.yarn/versions
_dev
.next
node_modules
.env
.env*.local
|
Beta Was this translation helpful? Give feedback.
-
For anyone else landing here from a search, the most straightforward solution to me seems plugin-production-install |
Beta Was this translation helpful? Give feedback.
I'm using @Dcard's yarn-plugin-docker-build to get optimized docker image in monorepo
It performs dependency analysis and integrates elegantly with the
yarn pack
script, leaving only minimal production artifacts.I got an image that was reduced by 60% compared to the previous image.
My Dockerfile looks like: