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 # Base Node.js image
# syntax=docker/dockerfile:1.4 FROM node:20-alpine as builder
# Base stage for common settings # Set working directory
FROM node:20-alpine AS base
WORKDIR /app WORKDIR /app
# Dependencies stage - caches npm install unless package.json changes # Copy package files for better caching
FROM base AS deps
COPY package.json package-lock.json ./ 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 # Install dependencies
FROM base AS builder RUN npm ci --force
WORKDIR /app
# Copy node_modules from deps stage # Install git and prepare for git hash capture
COPY --from=deps /app/node_modules ./node_modules
# Get git and create git hash file before copying source
RUN apk add --no-cache git RUN apk add --no-cache git
# Copy package files first (these change less frequently) # Copy source code
COPY package.json package-lock.json ./ COPY . .
COPY next.config.mjs tsconfig.json ./
# Copy source files (these change most frequently) # Get git commit hash or use "unknown" if not available
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 || :
RUN if [ -d .git ]; then \ RUN if [ -d .git ]; then \
git rev-parse --short HEAD > /app/git_commit_sha; \ git rev-parse --short HEAD > /app/git_commit_sha; \
else \ else \
echo "unknown" > /app/git_commit_sha; \ echo "unknown" > /app/git_commit_sha; \
fi fi
# Set build env vars # Set environment variables
ENV NEXT_PUBLIC_API_URL=/api ENV NEXT_PUBLIC_API_URL=/api
ENV NODE_ENV=production ENV NODE_ENV=production
# Build (using build cache when possible) # Build application with output standalone option
RUN --mount=type=cache,target=/app/.next/cache \ RUN echo "Building with commit $(cat /app/git_commit_sha)" && \
echo "Building with GIT_COMMIT_SHA=$(cat /app/git_commit_sha)" && \
npm run build npm run build
# Production stage # Production image
FROM node:20-alpine AS runner FROM node:20-alpine
WORKDIR /app WORKDIR /app
# Set production env vars # Set production env vars
@@ -65,7 +41,7 @@ ENV NODE_ENV=production
ENV NEXT_PUBLIC_API_URL=/api ENV NEXT_PUBLIC_API_URL=/api
ENV SERVER_API_URL=https://internal-api.inboxi.ng 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 && \ RUN addgroup --system --gid 1001 nodejs && \
adduser --system --uid 1001 nextjs 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/public ./public
COPY --from=builder /app/next.config.mjs ./ COPY --from=builder /app/next.config.mjs ./
COPY --from=builder /app/git_commit_sha /app/git_commit_sha COPY --from=builder /app/git_commit_sha /app/git_commit_sha
COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./ COPY --from=builder /app/.next/standalone ./
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static COPY --from=builder /app/.next/static ./.next/static
# Verify git hash is readable # Create startup script to set the git commit hash
RUN cat /app/git_commit_sha
# Instead of using ENV with command substitution, create a wrapper script
RUN echo '#!/bin/sh' > /app/start.sh && \ RUN echo '#!/bin/sh' > /app/start.sh && \
echo 'export GIT_COMMIT_SHA=$(cat /app/git_commit_sha)' >> /app/start.sh && \ echo 'export GIT_COMMIT_SHA=$(cat /app/git_commit_sha)' >> /app/start.sh && \
echo 'exec node server.js' >> /app/start.sh && \ echo 'exec node server.js' >> /app/start.sh && \
chmod +x /app/start.sh && \ chmod +x /app/start.sh
cat /app/start.sh
# Switch to non-root user # Switch to non-root user
USER nextjs USER nextjs
EXPOSE 3000 EXPOSE 3000
# Use the wrapper script # Use the wrapper script to set the git hash properly
CMD ["/app/start.sh"] CMD ["/app/start.sh"]