From 15ca17ce3bb9355aebc3cf600938d8cdf1c5892b Mon Sep 17 00:00:00 2001 From: Sina Alikhani Date: Tue, 9 Jun 2026 16:09:53 +0330 Subject: [PATCH] Chnage Dockerfile from single stage to multistage --- Dockerfile | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/Dockerfile b/Dockerfile index a33eb0e..03bf91a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,23 +1,23 @@ -# Use the official Node.js image as the base image -FROM docker.arvancloud.ir/node:18 +# ─── Stage 0: Base ──────────────────────────────────────────────────────────── +FROM node:20-alpine AS base +WORKDIR /app -# Set the working directory inside the container -WORKDIR /usr/src/app - -# Copy package.json and package-lock.json to the working directory +# ─── Stage 1: Dependencies ─────────────────────────────────────────────────── +FROM base AS deps COPY package*.json ./ +RUN npm ci -# Install the application dependencies -RUN npm install - -# Copy the rest of the application files +# ─── Stage 2: Builder ───────────────────────────────────────────────────────── +FROM deps AS builder COPY . . - -# Build the NestJS application RUN npm run build +RUN npm ci --omit=dev --ignore-scripts -# Expose the application port +# ─── Stage 3: Production ────────────────────────────────────────────────────── +FROM base AS production +ENV NODE_ENV=production +COPY --from=builder --chown=appuser:appgroup /app/dist ./dist +COPY --from=builder --chown=appuser:appgroup /app/node_modules ./node_modules +COPY --from=builder --chown=appuser:appgroup /app/package.json ./package.json EXPOSE 3000 - -# Command to run the application -CMD ["node", "dist/main"] +CMD ["node", "dist/main"] \ No newline at end of file