From 029ea19bfe37c18dc9f0198c46368a58c2dabc5f Mon Sep 17 00:00:00 2001 From: Varun-Kolanu Date: Mon, 21 Oct 2024 20:07:11 +0530 Subject: [PATCH] feat: optimize image size with multi-staged dockerfile --- Dockerfile | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/Dockerfile b/Dockerfile index f0d69e54df02..bfa43f063538 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,19 +1,27 @@ # Development Docker file -FROM node:18-alpine + +# Stage 1: Dependency fetching step +FROM node:18-alpine AS deps WORKDIR /async -# Install development dependencies +# Install dependencies COPY package.json package-lock.json ./ RUN npm install -# Copy the rest of the application files +# Stage 2: Development environment +FROM node:18-alpine AS dev + +WORKDIR /async + +# Copy only node_modules from dependency stage by avoiding temporary caches formed +COPY --from=deps /async/node_modules ./node_modules + COPY . . -# Expose the port for development (if needed) EXPOSE 3000 -# Set environment variables for development (optional) +# Set environment variables for development ENV NODE_ENV=development CMD ["npm", "run", "dev"]