Chnage Dockerfile from single stage to multistage

This commit is contained in:
2026-06-09 16:09:53 +03:30
parent 3af3805863
commit 15ca17ce3b

View File

@@ -1,23 +1,23 @@
# Use the official Node.js image as the base image # ─── Stage 0: Base ────────────────────────────────────────────────────────────
FROM docker.arvancloud.ir/node:18 FROM node:20-alpine AS base
WORKDIR /app
# Set the working directory inside the container # ─── Stage 1: Dependencies ───────────────────────────────────────────────────
WORKDIR /usr/src/app FROM base AS deps
# Copy package.json and package-lock.json to the working directory
COPY package*.json ./ COPY package*.json ./
RUN npm ci
# Install the application dependencies # ─── Stage 2: Builder ─────────────────────────────────────────────────────────
RUN npm install FROM deps AS builder
# Copy the rest of the application files
COPY . . COPY . .
# Build the NestJS application
RUN npm run build 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 EXPOSE 3000
CMD ["node", "dist/main"]
# Command to run the application
CMD ["node", "dist/main"]