be81aa1c8b
Vite reads env vars at build time, not runtime. The previous setup only set them in docker-compose 'environment' which is only available at container runtime. Now they are passed as build args so Vite can embed the correct API URL during the build process. - Add build args to web service in both compose files - Update Dockerfile to accept ARGs and set ENV for Vite - Fixes login redirect always going to localhost:8000
53 lines
1.2 KiB
Docker
53 lines
1.2 KiB
Docker
# Build stage
|
|
FROM node:20-alpine as builder
|
|
|
|
WORKDIR /build
|
|
|
|
# Copy package files
|
|
COPY package.json package-lock.json* ./
|
|
|
|
# Install dependencies
|
|
RUN npm install
|
|
|
|
# Copy source code
|
|
COPY . .
|
|
|
|
# Accept build arguments for API and app URLs
|
|
ARG VITE_API_BASE_URL
|
|
ARG VITE_APP_URL
|
|
ENV VITE_API_BASE_URL=${VITE_API_BASE_URL}
|
|
ENV VITE_APP_URL=${VITE_APP_URL}
|
|
|
|
# Build production bundle
|
|
RUN npm run build
|
|
|
|
# Production stage
|
|
FROM nginx:alpine
|
|
|
|
# Create non-root user
|
|
RUN addgroup -g 1001 -S nodejs && adduser -S nextjs -u 1001
|
|
|
|
# Copy custom nginx config
|
|
COPY nginx.conf /etc/nginx/conf.d/default.conf
|
|
|
|
# Copy built assets from builder
|
|
COPY --from=builder --chown=nextjs:nodejs /build/dist /usr/share/nginx/html
|
|
|
|
# Create required directories and fix permissions for non-root user
|
|
RUN mkdir -p /var/cache/nginx /var/run /run && \
|
|
chown -R nextjs:nodejs /var/cache/nginx /var/run /run /usr/share/nginx/html && \
|
|
chmod 755 /run /var/run
|
|
|
|
# Switch to non-root user
|
|
USER nextjs
|
|
|
|
# Expose port
|
|
EXPOSE 80
|
|
|
|
# Health check
|
|
HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \
|
|
CMD wget --quiet --tries=1 --spider http://127.0.0.1/ || exit 1
|
|
|
|
# Start nginx
|
|
CMD ["nginx", "-g", "daemon off;"]
|