diff --git a/docs/cli/fetch.md b/docs/cli/fetch.md index db0235d3dc8e..52a02da4b493 100644 --- a/docs/cli/fetch.md +++ b/docs/cli/fetch.md @@ -12,23 +12,28 @@ This command is specifically designed to improve building a docker image. You may have read the [official guide] to writing a Dockerfile for a Node.js app, if you haven't read it yet, you may want to read it first. +Also see [Working wirh Docker](../docker.md), for examples of multi-stage +builds, and `pnpm deploy` that can be combined with this command for a smaller +final image. + From that guide, we learn to write an optimized Dockerfile for projects using pnpm, which looks like ```Dockerfile FROM node:20 +ENV PNPM_HOME="/pnpm" +ENV PATH="$PNPM_HOME:$PATH" +RUN corepack enable pnpm && corepack install -g pnpm@latest-9 WORKDIR /path/to/somewhere -RUN corepack enable pnpm && corepack install -g pnpm@latest-9 - # Files required by pnpm install COPY .npmrc package.json pnpm-lock.yaml .pnpmfile.cjs ./ # If you patched any package, include patches before install too COPY patches patches -RUN pnpm install --frozen-lockfile --prod +RUN pnpm --mount=type=cache,id=pnpm,target=/pnpm/store install --frozen-lockfile --prod # Bundle app source COPY . . @@ -52,11 +57,12 @@ look like ```Dockerfile FROM node:20 +ENV PNPM_HOME="/pnpm" +ENV PATH="$PNPM_HOME:$PATH" +RUN corepack enable pnpm && corepack install -g pnpm@latest-9 WORKDIR /path/to/somewhere -RUN corepack enable pnpm && corepack install -g pnpm@latest-9 - # Files required by pnpm install COPY .npmrc package.json pnpm-lock.yaml .pnpmfile.cjs ./ @@ -69,7 +75,7 @@ COPY patches patches COPY packages/foo/package.json packages/foo/ COPY packages/bar/package.json packages/bar/ -RUN pnpm install --frozen-lockfile --prod +RUN --mount=type=cache,id=pnpm,target=/pnpm/store pnpm install --frozen-lockfile --prod # Bundle app source COPY . . @@ -86,18 +92,19 @@ to load packages into the virtual store using only information from a lockfile. ```Dockerfile FROM node:20 +ENV PNPM_HOME="/pnpm" +ENV PATH="$PNPM_HOME:$PATH" +RUN corepack enable pnpm && corepack install -g pnpm@latest-9 WORKDIR /path/to/somewhere -RUN corepack enable pnpm && corepack install -g pnpm@latest-9 - # pnpm fetch does require only lockfile COPY pnpm-lock.yaml ./ # If you patched any package, include patches before running pnpm fetch COPY patches patches -RUN pnpm fetch --prod +RUN --mount=type=cache,id=pnpm,target=/pnpm/store pnpm fetch --prod ADD . ./ diff --git a/docs/docker.md b/docs/docker.md index 408722b45fa9..7b9b6ccb7c7c 100644 --- a/docs/docker.md +++ b/docs/docker.md @@ -3,6 +3,8 @@ id: docker title: Working with Docker --- +Also see [pnpm fetch](./cli/fetch.md) for a command that can be used to speed up consecutive builds. + :::note It is impossible to create reflinks or hardlinks between a Docker container and the host filesystem during build time.