-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDockerfile
More file actions
80 lines (61 loc) · 2.93 KB
/
Copy pathDockerfile
File metadata and controls
80 lines (61 loc) · 2.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# Pin Node LTS (Active LTS) — same major everywhere (build + runtime).
ARG NODE_VERSION=24
# Dependencies
FROM node:${NODE_VERSION}-alpine AS deps
RUN apk add --no-cache libc6-compat
WORKDIR /app
# Install dependencies based on the preferred package manager
COPY package.json package-lock.json* ./
RUN \
if [ -f package-lock.json ]; then npm ci --frozen-lockfile; \
else echo "Lockfile not found." && exit 1; \
fi
# Builder
FROM node:${NODE_VERSION}-alpine AS builder
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY . .
ENV NEXT_TELEMETRY_DISABLED=1
RUN npm run build
# Prune node_modules to keep only production dependencies
FROM node:${NODE_VERSION}-alpine AS prod-deps
ENV NODE_ENV=production
RUN apk add --no-cache libc6-compat
WORKDIR /app
COPY package.json package-lock.json* ./
RUN npm ci --omit=dev --ignore-scripts && npm cache clean --force
# Lightweight cleanup stage to remove unnecessary files
FROM alpine:3.24 AS cleanup
WORKDIR /app
COPY --from=prod-deps /app/node_modules ./node_modules
# Remove unnecessary files from node_modules to reduce size
RUN find ./node_modules -type d -name "*.d.ts" -exec rm -rf {} + 2>/dev/null || true && \
find ./node_modules -type f \( -name "*.md" -o -name "*.txt" -o -name "LICENSE*" -o -name "*.map" -o -name "*.json" \) -delete && \
find ./node_modules -type d \( -name "test" -o -name "tests" -o -name "__tests__" -o -name "spec" -o -name ".github" -o -name "docs" -o -name "examples" \) -exec rm -rf {} + 2>/dev/null || true && \
find ./node_modules -type f \( -name "*.ts" -o -name "*.jsx" -o -name "*.mjs" \) ! -path "*/node_modules/.bin/*" -delete 2>/dev/null || true && \
find ./node_modules/@ckeditor -type f \( -name "*.map" -o -name "*.ts" -o -name "*.jsx" \) -delete 2>/dev/null || true && \
find ./node_modules -type d -name "node_modules" ! -path "./node_modules" -exec rm -rf {} + 2>/dev/null || true
# Runner — same Node image as build stages (no apk nodejs mismatch)
FROM node:${NODE_VERSION}-alpine AS runner
WORKDIR /app
ENV NODE_ENV="production"
ENV NEXT_TELEMETRY_DISABLED=1
RUN apk add --no-cache wget && \
addgroup --system --gid 1001 nodejs && \
adduser --system --uid 1001 --ingroup nodejs nextjs
# Copy cleaned node_modules
COPY --from=cleanup --chown=nextjs:nodejs /app/node_modules ./node_modules
# Copy only essential build outputs
COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static
COPY --from=builder --chown=nextjs:nodejs /app/public ./public
COPY --from=builder --chown=nextjs:nodejs /app/src/messages ./src/messages
# Strip unnecessary files
RUN rm -rf /usr/share/man/* /var/cache/apk/* /tmp/* 2>/dev/null || true
USER nextjs
EXPOSE 3000
ENV PORT=3000
ENV HOSTNAME="0.0.0.0"
HEALTHCHECK --interval=30s --timeout=5s --start-period=10s --retries=3 \
CMD wget --quiet --tries=1 --spider http://127.0.0.1:3000/env || exit 1
CMD ["node", "server.js"]