# Modern Dockerfile for Vite + React application # Build stage FROM node:24.9-alpine3.21 AS builder WORKDIR /app ARG OWN_NPM_TOKEN # Copy npm config required for private registry access. Ensure the build context includes .npmrc. COPY .npmrc .npmrc # Copy package files COPY package*.json ./ # Install dependencies (include devDependencies for Vite build) RUN npm ci RUN rm -f .npmrc # Copy source code COPY . . # Build the application ARG APP_SUBFOLDER="" ENV VITE_BASE_PATH=${APP_SUBFOLDER} RUN npm run build # Production stage - Use nginx for better performance FROM nginx:1.29.1-alpine # Remove default nginx website RUN rm -rf /usr/share/nginx/html/* # Create subfolder structure dynamically based on APP_SUBFOLDER ARG APP_SUBFOLDER="" RUN if [ -n "$APP_SUBFOLDER" ]; then mkdir -p /usr/share/nginx/html/$APP_SUBFOLDER; fi # Copy built application COPY --from=builder /app/build /usr/share/nginx/html/${APP_SUBFOLDER} # Copy and configure nginx COPY nginx/nginx.conf /etc/nginx/nginx.conf COPY nginx/configure-nginx.sh /usr/local/bin/configure-nginx.sh RUN chmod +x /usr/local/bin/configure-nginx.sh # Configure nginx.conf based on APP_SUBFOLDER RUN /usr/local/bin/configure-nginx.sh # Add environment variables ENV Author="Tudor Stanciu" ARG APP_VERSION=0.0.0 ENV APP_VERSION=${APP_VERSION} # Expose port 80 EXPOSE 80 # Health check HEALTHCHECK --interval=120s --timeout=10s --start-period=60s --retries=3 \ CMD if [ -n "$APP_SUBFOLDER" ]; then curl -f http://localhost/$APP_SUBFOLDER/ || exit 1; else curl -f http://localhost/ || exit 1; fi # Start nginx CMD ["nginx", "-g", "daemon off;"]