# syntax=docker/dockerfile:1.4

# Build stage
FROM node:18-alpine AS build

WORKDIR /app

# Update package database and install build dependencies
RUN apk update && apk add --no-cache python3 make g++ py3-pip

# Copy package files
COPY package.json package-lock.json ./

# Install dependencies
RUN npm install --legacy-peer-deps

# Copy source code
COPY . .

# Build the Gatsby app
RUN npm run build

# Production stage
FROM nginx:alpine AS production

# Copy nginx config
RUN echo 'server { listen 80; root /usr/share/nginx/html; index index.html; location / { try_files $uri $uri/ /index.html; } }' > /etc/nginx/conf.d/default.conf

# Copy built files
COPY --from=build /app/public /usr/share/nginx/html

EXPOSE 80

CMD ["nginx", "-g", "daemon off;"]