# Base Node.js image FROM node:20-alpine as builder # Set working directory WORKDIR /app # Copy package files for better caching COPY package.json package-lock.json ./ # Install dependencies RUN npm ci --force # Install git and prepare for git hash capture RUN apk add --no-cache git # Copy source code COPY . . # Get git commit hash or use "unknown" if not available RUN if [ -d .git ]; then \ git rev-parse --short HEAD > /app/git_commit_sha; \ else \ echo "unknown" > /app/git_commit_sha; \ fi # Set environment variables ENV NEXT_PUBLIC_API_URL=/api ENV NODE_ENV=production # Build application with output standalone option RUN echo "Building with commit $(cat /app/git_commit_sha)" && \ npm run build # Production image FROM node:20-alpine WORKDIR /app # Set production env vars ENV NODE_ENV=production ENV NEXT_PUBLIC_API_URL=/api ENV SERVER_API_URL=https://internal-api.inboxi.ng # Create non-root user RUN addgroup --system --gid 1001 nodejs && \ adduser --system --uid 1001 nextjs # Copy only necessary files COPY --from=builder /app/public ./public COPY --from=builder /app/next.config.mjs ./ COPY --from=builder /app/git_commit_sha /app/git_commit_sha COPY --from=builder /app/.next/standalone ./ COPY --from=builder /app/.next/static ./.next/static # Create startup script to set the git commit hash RUN echo '#!/bin/sh' > /app/start.sh && \ echo 'export GIT_COMMIT_SHA=$(cat /app/git_commit_sha)' >> /app/start.sh && \ echo 'exec node server.js' >> /app/start.sh && \ chmod +x /app/start.sh # Switch to non-root user USER nextjs EXPOSE 3000 # Use the wrapper script to set the git hash properly CMD ["/app/start.sh"]