Dockerfile improvement

This commit is contained in:
Stefan Hardegger
2025-07-28 14:28:01 +02:00
parent a501b27169
commit 860bf02d56
4 changed files with 71 additions and 2 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 37 KiB

View File

@@ -1,13 +1,40 @@
# Use node 18 alpine for smaller image size
FROM node:18-alpine FROM node:18-alpine
WORKDIR /app WORKDIR /app
COPY package*.json ./ # Install dumb-init for proper signal handling
RUN npm ci --omit=dev RUN apk add --no-cache dumb-init
# Copy package files
COPY package*.json ./
# Install all dependencies (including devDependencies needed for build)
# Set npm config for better CI performance
RUN npm ci --prefer-offline --no-audit
# Copy source code
COPY . . COPY . .
# Set Node.js memory limit for build (helpful in constrained environments)
ENV NODE_OPTIONS="--max-old-space-size=1024"
# Build the application
RUN npm run build RUN npm run build
# Remove devDependencies after build to reduce image size
RUN npm prune --omit=dev
# Create non-root user for security
RUN addgroup -g 1001 -S nodejs
RUN adduser -S nextjs -u 1001
# Change ownership of the app directory
RUN chown -R nextjs:nodejs /app
USER nextjs
EXPOSE 3000 EXPOSE 3000
# Use dumb-init to handle signals properly
ENTRYPOINT ["dumb-init", "--"]
CMD ["npm", "start"] CMD ["npm", "start"]

View File

@@ -0,0 +1,42 @@
# Multi-stage build for better caching and smaller final image
FROM node:18-alpine AS dependencies
WORKDIR /app
COPY package*.json ./
RUN npm ci
FROM node:18-alpine AS builder
WORKDIR /app
COPY --from=dependencies /app/node_modules ./node_modules
COPY . .
# Increase memory limit for build
ENV NODE_OPTIONS="--max-old-space-size=2048"
RUN npm run build
FROM node:18-alpine AS runner
WORKDIR /app
# Install dumb-init
RUN apk add --no-cache dumb-init
# Create non-root user
RUN addgroup -g 1001 -S nodejs
RUN adduser -S nextjs -u 1001
# Copy necessary files
COPY --from=builder /app/public ./public
COPY --from=builder /app/.next/standalone ./
COPY --from=builder /app/.next/static ./.next/static
# Set correct permissions
RUN chown -R nextjs:nodejs /app
USER nextjs
EXPOSE 3000
ENTRYPOINT ["dumb-init", "--"]
CMD ["node", "server.js"]

Binary file not shown.