sameernotes commited on
Commit
c8ae960
·
verified ·
1 Parent(s): 7f8f884

Update Dockerfile

Browse files
Files changed (1) hide show
  1. Dockerfile +30 -16
Dockerfile CHANGED
@@ -1,31 +1,45 @@
1
- # for deploying a React application with Node.js in a Docker container
 
 
2
 
3
- # Use Node 22 as the base image
4
- FROM node:22-alpine
5
-
6
- # Set the working directory inside the container
7
  WORKDIR /app
8
 
9
- # Copy package.json and package-lock.json (if you have one)
10
  COPY package*.json ./
11
-
12
- # Install dependencies
13
  RUN npm install
14
 
15
- # Copy the rest of the application code
16
  COPY . .
17
 
18
- # Expose the port your React app runs on
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
  EXPOSE 7860
20
 
21
- # Change ownership of /app to the node user
22
  RUN chown -R node:node /app
23
 
24
- # Switch to the node user
25
  USER node
26
 
27
- # Command to build the application
28
- CMD ["npm", "run", "build"]
29
-
30
- # Command to run the application
31
  CMD ["npm", "run", "start"]
 
1
+ # === Stage 1: Build ===
2
+ # Create a build environment
3
+ FROM node:22-alpine AS builder
4
 
5
+ # Set working directory
 
 
 
6
  WORKDIR /app
7
 
8
+ # Copy package files and install ALL dependencies (including devDependencies)
9
  COPY package*.json ./
 
 
10
  RUN npm install
11
 
12
+ # Copy the rest of the source code
13
  COPY . .
14
 
15
+ # Build the application
16
+ # Use --no-cache to ensure a clean build
17
+ RUN npm run build --no-cache
18
+
19
+ # === Stage 2: Production ===
20
+ # Create a final, lean production image
21
+ FROM node:22-alpine
22
+
23
+ # Set working directory
24
+ WORKDIR /app
25
+
26
+ # Install ONLY production dependencies
27
+ COPY package*.json ./
28
+ RUN npm install --omit=dev
29
+
30
+ # Copy assets and build output from the builder stage
31
+ COPY --from=builder /app/public ./public
32
+ COPY --from=builder /app/.next ./.next
33
+ COPY --from=builder /app/next.config.mjs ./next.config.mjs
34
+
35
+ # Expose the port
36
  EXPOSE 7860
37
 
38
+ # Change ownership to the node user for security
39
  RUN chown -R node:node /app
40
 
41
+ # Switch to the non-root user
42
  USER node
43
 
44
+ # The command to start the application
 
 
 
45
  CMD ["npm", "run", "start"]