This commit is contained in:
NotII
2025-04-06 16:23:02 +01:00
parent d05d7b4938
commit 8da4d0085e
3 changed files with 36 additions and 85 deletions

View File

@@ -1,67 +1,48 @@
# Base Node.js image
FROM node:20-alpine as builder
# Set working directory
# Use official Node.js image as base
FROM node:18-alpine AS builder
WORKDIR /app
# Copy package files for better caching
COPY package.json package-lock.json ./
# Install dependencies (using install instead of ci to handle mismatched lock file)
RUN npm install --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
# Install git, get commit hash
RUN apk add --no-cache git \
&& git rev-parse --short HEAD > /app/git_commit_sha
# 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
# Build the Next.js application
# The environment variable will be available during build if needed
# ENV GIT_COMMIT_SHA=$(cat /app/git_commit_sha)
RUN echo "Building with GIT_COMMIT_SHA=$(cat /app/git_commit_sha)" && npm run build
# Production image
FROM node:20-alpine
# ---- Production Stage ----
FROM node:18-alpine AS runner
# Set working directory inside the container
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
RUN mkdir -p /app/public
# Create non-root user
RUN addgroup --system --gid 1001 nodejs && \
adduser --system --uid 1001 nextjs
# Copy only necessary files from builder
COPY --from=builder /app/package.json /app/package-lock.json ./
COPY --from=builder /app/.next ./.next
COPY --from=builder /app/node_modules ./node_modules
# Copy only necessary files
COPY --from=builder /app/public ./public
COPY --from=builder /app/next.config.mjs ./
# Copy commit hash file from builder stage
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"]
ENV NODE_ENV=production
ENV NEXT_PUBLIC_API_URL=/api
ENV SERVER_API_URL=https://internal-api.inboxi.ng/api
# Set GIT_COMMIT_SHA environment variable in the final image by reading the file
ENV GIT_COMMIT_SHA="$(cat /app/git_commit_sha)"
# Start Next.js server
CMD ["npm", "run", "start"]