Enhance Docker setup by updating .dockerignore to include additional files and directories, modifying docker-compose.yml to add build arguments and Traefik labels, and improving the Dockerfile with build-time environment variables, health checks, and permission settings for security.

This commit is contained in:
2025-08-15 20:40:39 +01:00
parent ef211ebe0a
commit f7dc75d514
4 changed files with 340 additions and 41 deletions

View File

@ -1,31 +1,66 @@
# Build stage
FROM node:18-alpine AS builder
# Install build dependencies for native modules (bcrypt, etc.)
RUN apk add --no-cache python3 make g++ libc6-compat
WORKDIR /app
# Copy package files
# Copy package files first for better Docker layer caching
COPY package*.json ./
# Install ALL dependencies (including dev dependencies needed for build)
RUN npm ci
# Install dependencies with proper npm cache handling
RUN npm ci --only=production=false --silent
# Copy source code
COPY . .
# Set build-time environment variables
ARG VITE_APP_NAME="ScriptShare"
ARG VITE_APP_URL="https://scriptshare.example.com"
ARG VITE_ANALYTICS_ENABLED="false"
# Export as environment variables for Vite build
ENV VITE_APP_NAME=$VITE_APP_NAME
ENV VITE_APP_URL=$VITE_APP_URL
ENV VITE_ANALYTICS_ENABLED=$VITE_ANALYTICS_ENABLED
# Build the application
RUN npm run build
# Verify build output exists
RUN ls -la /app/dist
# Production stage
FROM nginx:alpine
# Install curl for health checks
RUN apk add --no-cache curl
# Copy built files from builder stage
COPY --from=builder /app/dist /usr/share/nginx/html
# Copy nginx configuration
COPY nginx.conf /etc/nginx/nginx.conf
# Create nginx pid directory
RUN mkdir -p /var/run/nginx
# Set proper permissions
RUN chown -R nginx:nginx /usr/share/nginx/html
RUN chown -R nginx:nginx /var/cache/nginx
RUN chown -R nginx:nginx /var/log/nginx
RUN chown -R nginx:nginx /var/run/nginx
# Switch to non-root user for security
USER nginx
# Expose port 80
EXPOSE 80
# Add healthcheck
HEALTHCHECK --interval=30s --timeout=10s --start-period=40s --retries=3 \
CMD curl -f http://localhost/health || exit 1
# Start nginx
CMD ["nginx", "-g", "daemon off;"]