Merge pull request 'refactor: Transition production Dockerfile to use Nginx for serving built assets and streamline environment variable handling' (#18) from ph4 into prod
Reviewed-on: #18
This commit is contained in:
commit
028c991d91
@ -29,43 +29,30 @@ RUN npm ci
|
|||||||
COPY . .
|
COPY . .
|
||||||
|
|
||||||
# Set environment variables for build
|
# Set environment variables for build
|
||||||
ENV NODE_ENV=production
|
ARG VITE_API_URL
|
||||||
|
ENV VITE_API_URL=${VITE_API_URL}
|
||||||
|
|
||||||
# Build the application
|
# Build the application
|
||||||
RUN npm run build-only
|
RUN npm run build-only
|
||||||
|
|
||||||
# Production stage
|
# Production stage
|
||||||
FROM node:slim AS production
|
FROM nginx:alpine
|
||||||
|
|
||||||
# Install serve globally
|
|
||||||
RUN npm install -g serve
|
|
||||||
|
|
||||||
# Set working directory
|
|
||||||
WORKDIR /app
|
|
||||||
|
|
||||||
# Copy built assets from build stage
|
# Copy built assets from build stage
|
||||||
COPY --from=build /app/dist .
|
COPY --from=build /app/dist /usr/share/nginx/html
|
||||||
|
|
||||||
# Create a default static.json for serve to handle SPA routing
|
# Copy nginx configuration for SPA routing
|
||||||
RUN echo '{ \n "rewrites": [ \n { "source": "**", "destination": "/index.html" } \n ] \n}' > static.json
|
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 script to inject environment variables at runtime
|
# Expose port 80
|
||||||
RUN echo '#!/bin/sh\n\
|
EXPOSE 80
|
||||||
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
|
|
||||||
|
|
||||||
# Expose port 3000 (serve default)
|
# Start nginx
|
||||||
EXPOSE 3000
|
CMD ["nginx", "-g", "daemon off;"]
|
||||||
|
|
||||||
# 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"]
|
|
Loading…
Reference in New Issue
Block a user