Update Dockerfile

This commit is contained in:
NotII
2025-04-06 16:12:20 +01:00
parent a5f46d68f0
commit adf65dc6e7

View File

@@ -1,63 +1,39 @@
# Use BuildKit for better caching
# syntax=docker/dockerfile:1.4
# Base Node.js image
FROM node:20-alpine as builder
# Base stage for common settings
FROM node:20-alpine AS base
# Set working directory
WORKDIR /app
# Dependencies stage - caches npm install unless package.json changes
FROM base AS deps
# Copy package files for better caching
COPY package.json package-lock.json ./
RUN --mount=type=cache,target=/root/.npm \
npm ci --force --prefer-offline
# Build stage - only rebuilds when source files change
FROM base AS builder
WORKDIR /app
# Install dependencies
RUN npm ci --force
# Copy node_modules from deps stage
COPY --from=deps /app/node_modules ./node_modules
# Get git and create git hash file before copying source
# Install git and prepare for git hash capture
RUN apk add --no-cache git
# Copy package files first (these change less frequently)
COPY package.json package-lock.json ./
COPY next.config.mjs tsconfig.json ./
# Copy source code
COPY . .
# Copy source files (these change most frequently)
COPY public ./public
COPY app ./app
COPY components ./components
COPY lib ./lib
COPY config ./config
COPY services ./services
COPY utils ./utils
COPY middleware.ts ./
COPY .env* ./
COPY styles* ./
COPY hooks ./hooks
COPY models ./models
# Copy git directory if it exists or create dummy hash
COPY .git ./.git 2>/dev/null || :
# 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 build env vars
# Set environment variables
ENV NEXT_PUBLIC_API_URL=/api
ENV NODE_ENV=production
# Build (using build cache when possible)
RUN --mount=type=cache,target=/app/.next/cache \
echo "Building with GIT_COMMIT_SHA=$(cat /app/git_commit_sha)" && \
# Build application with output standalone option
RUN echo "Building with commit $(cat /app/git_commit_sha)" && \
npm run build
# Production stage
FROM node:20-alpine AS runner
# Production image
FROM node:20-alpine
WORKDIR /app
# Set production env vars
@@ -65,7 +41,7 @@ ENV NODE_ENV=production
ENV NEXT_PUBLIC_API_URL=/api
ENV SERVER_API_URL=https://internal-api.inboxi.ng
# Create non-root user for security
# Create non-root user
RUN addgroup --system --gid 1001 nodejs && \
adduser --system --uid 1001 nextjs
@@ -73,23 +49,19 @@ RUN addgroup --system --gid 1001 nodejs && \
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 --chown=nextjs:nodejs /app/.next/standalone ./
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static
COPY --from=builder /app/.next/standalone ./
COPY --from=builder /app/.next/static ./.next/static
# Verify git hash is readable
RUN cat /app/git_commit_sha
# Instead of using ENV with command substitution, create a wrapper script
# 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 && \
cat /app/start.sh
chmod +x /app/start.sh
# Switch to non-root user
USER nextjs
EXPOSE 3000
# Use the wrapper script
# Use the wrapper script to set the git hash properly
CMD ["/app/start.sh"]