File size: 1,506 Bytes
343b64b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9d3c91f
 
 
 
 
 
 
 
 
 
 
87d3d61
 
 
 
 
 
 
343b64b
 
 
 
9d3c91f
343b64b
9d3c91f
 
cc0f83e
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# Stage 1: Build the React app
FROM node:16-alpine AS build

# Set working directory
WORKDIR /app

# Copy package.json and package-lock.json
COPY package*.json ./

# Install dependencies
RUN npm install

# Copy the rest of the application code
COPY . .

# Build the React app
RUN npm run build

# Stage 2: Serve the built app with a lightweight web server
FROM nginx:alpine

# Install necessary packages and set up a non-root user
RUN apk add --no-cache shadow \
  && useradd -u 1001 -U -d /home/user -s /bin/bash user \
  && mkdir -p /home/user \
  && chown -R user:user /home/user

# Install nginx and set permissions
RUN apk add --no-cache nginx \
  && mkdir -p /var/cache/nginx /var/log/nginx /var/lib/nginx /var/run \
  && touch /var/run/nginx.pid \
  && chown -R user:user /var/cache/nginx /var/log/nginx /var/lib/nginx /var/run/nginx.pid

# Switch to the root user to perform privileged operations
USER root

# Remove the default Nginx configuration file
RUN rm /etc/nginx/conf.d/default.conf

# Copy the built files from the previous stage
COPY --from=build /app/dist /usr/share/nginx/html

# Replace the default nginx.conf with our configuration
COPY nginx.conf /etc/nginx/conf.d

# Change ownership of the necessary directories and files to the non-root user
RUN chown -R user:user /usr/share/nginx/html /etc/nginx/conf.d /var/cache/nginx /var/run /var/log/nginx

# Switch back to the "user" user
USER user

# Expose port 7860
EXPOSE 7860

# Start Nginx server
CMD ["nginx", "-g", "daemon off;"]