๐ฉโ๐ป Multi-stage Build
็บไฝ่ฆไฝฟ็จ Multi-stage Buildโ
ๅจ็ดๆฅๅปบ็ซ nest ็ docker image ๆ๏ผ
image ่ณๅฐๆ 400 ๅค mb๏ผๅฆๆ่ฆ้ฒ่ก็ฆ่บซๆๅ ้ฃๅขๅฟ ้่ฆๅท่กๆญคๅไฝ
่ฉฆๅ nest Multi-stage Buildโ
dockerfileโ
ๆญคๆญฅ้ฉๆฏๅ ไฝฟ็จ้็ผ็ฐๅข้ฒ่กๆๅ ๏ผๆฅไธไพไพ้ pnpm ไพๅฎ่ฃ็็ข็ฐๅขๆ้่ฆ็จๅฐ็ไพ่ณด
# development image
FROM node:alpine AS development
# ๅปบ็ซapp็ฎ้
WORKDIR /usr/src/app
# ่ค่ฃฝไพ่ณดๆช
COPY package*.json ./
COPY tsconfig*.json ./
# ไธ่ผ pnpm
RUN npm i -g pnpm
# ่ค่ฃฝ็จๅผ็ขผ
COPY . . /usr/src/app/
# ไธ่ผไพ่ณด
RUN pnpm install -r
# ้จๅฑฌๅฐๆก
RUN pnpm run build
# production image
FROM node:alpine AS production
# ๅปบ็ซapp็ฎ้
WORKDIR /usr/src/app
# ่ค่ฃฝไพ่ณดๆช
COPY package.json ./
COPY pnpm-lock.yaml ./
# ไธ่ผ pnpm
RUN npm i -g pnpm
# ไธ่ผไพ่ณด(ๅชไธ่ผ้ไฝฟ็จ็ไพ่ณด)
RUN pnpm install --prod
# ่ค่ฃฝๅๅๆๅ
็ๆชๆก
COPY --from=development /usr/src/app/dist ./dist
# ๅๅๆๅ
CMD [ "node", "dist/main.js" ]
ref: