Dockerfile raw

   1  # Step 1: Build the application
   2  FROM node:20-alpine as builder
   3  
   4  ARG VITE_PROXY_SERVER
   5  ENV VITE_PROXY_SERVER=${VITE_PROXY_SERVER}
   6  
   7  WORKDIR /app
   8  
   9  # Copy package files first
  10  COPY package*.json ./
  11  RUN npm install
  12  
  13  # Copy the source code to prevent invaliding cache whenever there is a change in the code
  14  COPY . .
  15  RUN npm run build
  16  
  17  # Step 2: Final container with Nginx and embedded config
  18  FROM nginx:alpine
  19  
  20  # Copy only the generated static files
  21  COPY --from=builder /app/dist /usr/share/nginx/html
  22  
  23  # Embed Nginx configuration directly
  24  RUN printf "server {\n\
  25      listen 80;\n\
  26      server_name localhost;\n\
  27      root /usr/share/nginx/html;\n\
  28      index index.html;\n\
  29  \n\
  30      location / {\n\
  31          try_files \$uri \$uri/ /index.html;\n\
  32      }\n\
  33  \n\
  34      location ~* \\.(?:js|css|woff2?|ttf|otf|eot|ico|jpg|jpeg|png|gif|svg|webp)\$ {\n\
  35          expires 30d;\n\
  36          access_log off;\n\
  37          add_header Cache-Control \"public\";\n\
  38      }\n\
  39  \n\
  40      gzip on;\n\
  41      gzip_types text/plain application/javascript application/x-javascript text/javascript text/css application/json;\n\
  42      gzip_proxied any;\n\
  43      gzip_min_length 1024;\n\
  44      gzip_comp_level 6;\n\
  45  }\n" > /etc/nginx/conf.d/default.conf
  46  
  47  EXPOSE 80
  48  
  49  CMD ["nginx", "-g", "daemon off;"]
  50