From c8b0dc647c33f5ff2be5162bad61dd4dd987db93 Mon Sep 17 00:00:00 2001 From: Gabriel Oliveira Date: Sun, 11 Aug 2024 20:50:05 -0300 Subject: [PATCH] docs: improve working with cache doc (#565) * docs: improve working with cache doc * improve * typo --- docs/docker.md | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/docs/docker.md b/docs/docker.md index 8e27f0f2e948..408722b45fa9 100644 --- a/docs/docker.md +++ b/docs/docker.md @@ -133,3 +133,32 @@ Run the following commands to build images for app1 and app2: docker build . --target app1 --tag app1:latest docker build . --target app2 --tag app2:latest ``` + +### Example 3: Build on CI/CD + +On CI or CD environments, the BuildKit cache mounts might not be available, because the VM or container is ephemeral and only normal docker cache will work. + +So an alternative is to use a typical Dockerfile with layers that are built incrementally, for this scenario, `pnpm fetch` is the best option, as it only needs the `pnpm-lock.yaml` file and the layer cache will only be lost when you change the dependencies. + +```dockerfile title="Dockerfile" +FROM node:20-slim AS base + +ENV PNPM_HOME="/pnpm" +ENV PATH="$PNPM_HOME:$PATH" +RUN corepack enable + +FROM base AS prod + +COPY pnpm-lock.yaml /app +WORKDIR /app +RUN pnpm fetch --prod + +COPY . /app +RUN pnpm run build + +FROM base +COPY --from=prod /app/node_modules /app/node_modules +COPY --from=prod /app/dist /app/dist +EXPOSE 8000 +CMD [ "pnpm", "start" ] +```