1- # Use a slim Node.js image as base
2- FROM node:20-alpine
1+ # ------------------------
2+ # Stage 1: Build Stage
3+ # ------------------------
4+ FROM node:20-alpine AS builder
35
4- # Install docker CLI to execute docker command (for accessing host Docker)
5- # Also install bash for better scripting capabilities inside the container if needed
6- RUN apk add --no-cache docker-cli bash
7-
8- # Set working directory inside the container
6+ # Set working directory
97WORKDIR /app
108
11- # Copy only package.json and package-lock.json to leverage Docker layer caching
12- # This ensures npm install is re-run only if dependencies change
9+ # Copy only package files to install dependencies first (cache-friendly)
1310COPY package*.json ./
1411
15- # Install project dependencies
16- RUN npm install
12+ # Install dependencies
13+ RUN npm ci --omit=dev
1714
18- # Copy the rest of your application source code
15+ # Copy the rest of the app source code
1916COPY . .
2017
21- # Set environment variables
22- ENV PORT=9091
18+ # ------------------------
19+ # Stage 2: Production Image
20+ # ------------------------
21+ FROM node:20-alpine AS production
22+
23+ # Set working directory
24+ WORKDIR /app
25+
26+ # Copy only the necessary files from builder
27+ COPY --from=builder /app/node_modules ./node_modules
28+ COPY --from=builder /app/package*.json ./
29+ COPY --from=builder /app/dist ./dist
30+ COPY --from=builder /app/temp ./temp
31+ COPY --from=builder /app/public ./public
32+ COPY --from=builder /app/*.js ./
2333
24- # Create a temporary directory that will be used for mounting (if it doesn't exist on host)
25- # This command ensures the directory exists within the image,
26- # but the volume mount `./temp:/app/temp` will override it with the host's `./temp`
27- # if `./temp` exists on the host. If `./temp` doesn't exist on the host, Docker will create it.
28- RUN mkdir -p /app/temp
34+ # Set environment variables
35+ ENV NODE_ENV=production
36+ ENV PORT=5000
2937
30- # Expose the port your Node.js application listens on
31- EXPOSE 9091
38+ # Expose app port
39+ EXPOSE 5000
3240
33- # Command to run your application when the container starts
34- CMD ["npm " , "start " ]
41+ # Start the application
42+ CMD ["node " , "index.js " ]
0 commit comments