Files
bakery-ia/frontend/Dockerfile.kubernetes
2025-09-27 14:51:06 +02:00

104 lines
3.2 KiB
Docker

# Kubernetes-optimized Dockerfile for Frontend
# Multi-stage build for production deployment
# Stage 1: Build the application
FROM node:18-alpine AS builder
WORKDIR /app
# Copy package files
COPY package*.json ./
# Install dependencies including dev dependencies for building
RUN npm ci --verbose && \
npm cache clean --force
# Copy source code
COPY . .
# Build the application for production
# This will use environment variables available at build time
RUN npm run build
# Stage 2: Production server with Nginx
FROM nginx:1.25-alpine AS production
# Install curl for health checks
RUN apk add --no-cache curl
# Remove default nginx configuration
RUN rm /etc/nginx/conf.d/default.conf
# Copy custom nginx configuration
COPY nginx.conf /etc/nginx/conf.d/
# Copy built application from builder stage
COPY --from=builder /app/dist /usr/share/nginx/html
# Create a script to substitute environment variables at runtime
COPY <<'EOF' /docker-entrypoint.d/30-substitute-env.sh
#!/bin/sh
set -e
# Handle VITE_API_URL specially to preserve empty values
# If VITE_API_URL is unset, use default; if empty, preserve empty; otherwise use value
if [ -z "${VITE_API_URL+x}" ]; then
export VITE_API_URL="http://gateway-service:8000"
elif [ -z "$VITE_API_URL" ]; then
# If VITE_API_URL is explicitly set to empty string, preserve it
export VITE_API_URL=""
fi
# Default values for other environment variables
export VITE_APP_TITLE=${VITE_APP_TITLE:-"PanIA Dashboard"}
export VITE_APP_VERSION=${VITE_APP_VERSION:-"1.0.0"}
# Create a runtime configuration file that can be loaded by the frontend
cat > /usr/share/nginx/html/runtime-config.js << EOL
window.__RUNTIME_CONFIG__ = {
VITE_API_URL: '${VITE_API_URL}',
VITE_APP_TITLE: '${VITE_APP_TITLE}',
VITE_APP_VERSION: '${VITE_APP_VERSION}'
};
EOL
echo "Runtime configuration created with API URL: ${VITE_API_URL}"
EOF
# Make the script executable
RUN chmod +x /docker-entrypoint.d/30-substitute-env.sh
# Set proper permissions
RUN chown -R nginx:nginx /usr/share/nginx/html && \
chown -R nginx:nginx /var/cache/nginx && \
chown -R nginx:nginx /var/log/nginx && \
chown -R nginx:nginx /etc/nginx/conf.d
# Create nginx PID directory and fix permissions
RUN mkdir -p /var/run/nginx /var/lib/nginx/tmp && \
chown -R nginx:nginx /var/run/nginx /var/lib/nginx
# Custom nginx.conf for running as non-root
RUN echo 'pid /var/run/nginx/nginx.pid;' > /etc/nginx/nginx.conf && \
echo 'events { worker_connections 1024; }' >> /etc/nginx/nginx.conf && \
echo 'http {' >> /etc/nginx/nginx.conf && \
echo ' include /etc/nginx/mime.types;' >> /etc/nginx/nginx.conf && \
echo ' default_type application/octet-stream;' >> /etc/nginx/nginx.conf && \
echo ' sendfile on;' >> /etc/nginx/nginx.conf && \
echo ' keepalive_timeout 65;' >> /etc/nginx/nginx.conf && \
echo ' include /etc/nginx/conf.d/*.conf;' >> /etc/nginx/nginx.conf && \
echo '}' >> /etc/nginx/nginx.conf
# Switch to non-root user
USER nginx
# Expose port 3000 (to match current setup)
EXPOSE 3000
# Health check
HEALTHCHECK --interval=30s --timeout=10s --start-period=30s --retries=3 \
CMD curl -f http://localhost:3000/health || exit 1
# Start nginx
CMD ["nginx", "-g", "daemon off;"]