From 9b09b461bd85b0a82be9fb47a199630faec1238b Mon Sep 17 00:00:00 2001 From: mohamad Date: Sun, 1 Jun 2025 16:46:00 +0200 Subject: [PATCH] refactor: Update production Dockerfile to use Node.js for serving built assets and enhance environment variable injection --- fe/Dockerfile.prod | 47 +++++++++++++++++++++++-------------- fe/src/config/api-config.ts | 2 +- 2 files changed, 31 insertions(+), 18 deletions(-) diff --git a/fe/Dockerfile.prod b/fe/Dockerfile.prod index c47dcb8..a0c2320 100644 --- a/fe/Dockerfile.prod +++ b/fe/Dockerfile.prod @@ -29,30 +29,43 @@ RUN npm ci COPY . . # Set environment variables for build -ARG VITE_API_URL -ENV VITE_API_URL=${VITE_API_URL} +ENV NODE_ENV=production # Build the application RUN npm run build-only # Production stage -FROM nginx:alpine +FROM node:slim AS production + +# Install serve globally +RUN npm install -g serve + +# Set working directory +WORKDIR /app # Copy built assets from build stage -COPY --from=build /app/dist /usr/share/nginx/html +COPY --from=build /app/dist . -# Copy nginx configuration for SPA routing -RUN echo 'server { \ - listen 80; \ - location / { \ - root /usr/share/nginx/html; \ - index index.html; \ - try_files $uri $uri/ /index.html; \ - } \ -}' > /etc/nginx/conf.d/default.conf +# Create a default static.json for serve to handle SPA routing +RUN echo '{ \n "rewrites": [ \n { "source": "**", "destination": "/index.html" } \n ] \n}' > static.json -# Expose port 80 -EXPOSE 80 +# Create a script to inject environment variables at runtime +RUN echo '#!/bin/sh\n\ +cat > /app/env-config.js << EOL\n\ +window.ENV = {\n\ + VITE_API_URL: "${VITE_API_URL}",\n\ + VITE_SENTRY_DSN: "${VITE_SENTRY_DSN}",\n\ + VITE_ROUTER_MODE: "${VITE_ROUTER_MODE}"\n\ +};\n\ +EOL\n\ +serve -s . -l 3000' > /app/start.sh && chmod +x /app/start.sh -# Start nginx -CMD ["nginx", "-g", "daemon off;"] \ No newline at end of file +# Expose port 3000 (serve default) +EXPOSE 3000 + +# Health check (optional, depends on serve capabilities or custom health endpoint) +# HEALTHCHECK --interval=30s --timeout=10s --start-period=30s --retries=3 \ +# CMD curl -f http://localhost:3000/ || exit 1 + +# Start serve with environment variable injection +CMD ["/app/start.sh"] \ No newline at end of file diff --git a/fe/src/config/api-config.ts b/fe/src/config/api-config.ts index 64db3d4..215d969 100644 --- a/fe/src/config/api-config.ts +++ b/fe/src/config/api-config.ts @@ -2,7 +2,7 @@ export const API_VERSION = 'v1' // API Base URL -export const API_BASE_URL = import.meta.env.VITE_API_URL || 'http://mitlistbe:8000' +export const API_BASE_URL = (window as any).ENV?.VITE_API_URL || 'http://mitlistbe:8000' // API Endpoints export const API_ENDPOINTS = {