chore: 优化 docker runtime 分段构建

This commit is contained in:
WJG 2024-06-02 11:49:07 +08:00
parent 020c7030d2
commit cfb63fe6f9
No known key found for this signature in database
GPG Key ID: 258474EF8590014A
2 changed files with 13 additions and 7 deletions

View File

@ -1,14 +1,16 @@
# 排除全部文件 # 排除全部文件
**/* **/*
# 只保留相关构建文件 # 只保留构建相关的文件
!src !src
!prisma
!app.js !app.js
!package.json !package.json
!pnpm-lock.yaml !pnpm-lock.yaml
!tsconfig.json !tsconfig.json
!tsup.config.ts !tsup.config.ts
!prisma/engines
!prisma/migrations
!prisma/schema.prisma

View File

@ -10,20 +10,24 @@ FROM env-$TARGETARCH as base
WORKDIR /app WORKDIR /app
ARG TARGETARCH ARG TARGETARCH
FROM base as build FROM base as runtime
COPY . . COPY . .
RUN [ ! "$TARGETARCH" = "arm" ] && rm -rf ./prisma/engines || true RUN [ ! "$TARGETARCH" = "arm" ] && rm -rf ./prisma/engines || true
RUN --mount=type=cache,target=/root/.npm \ RUN --mount=type=cache,target=/root/.npm \
npm install -g pnpm@9.1.1 npm install -g pnpm@9.1.1
RUN --mount=type=cache,target=/root/.local/share/pnpm/store \ RUN --mount=type=cache,target=/root/.local/share/pnpm/store \
pnpm install && pnpm build pnpm install --production && pnpm prisma generate
FROM runtime as dist
RUN --mount=type=cache,target=/root/.local/share/pnpm/store \
pnpm install && pnpm tsup
FROM base as release FROM base as release
COPY app.js . COPY app.js .
COPY package.json . COPY package.json .
COPY --from=build /app/dist ./dist COPY --from=dist /app/dist ./dist
COPY --from=build /app/prisma ./prisma COPY --from=dist /app/prisma ./prisma
COPY --from=build /app/node_modules ./node_modules COPY --from=runtime /app/node_modules ./node_modules
CMD npm run start CMD npm run start