Repository navigation
Expand file tree
/
Copy pathDockerfile
More file actions
76 lines (57 loc) · 2.14 KB
/
Copy pathDockerfile
File metadata and controls
76 lines (57 loc) · 2.14 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
# Build target dependencies #
###########################
FROM node:24-alpine AS base
WORKDIR /app
ARG NODE_ENV=production
ENV PATH=/app/node_modules/.bin:$PATH \
NODE_ENV="$NODE_ENV"
# Yarn
RUN yarn set version berry
COPY package.json yarn.lock* .yarnrc.yml ./
FROM base AS deps
COPY .yarn .yarn
RUN yarn workspaces focus --all --production
# Build target builder #
########################
FROM base AS builder
# Setup build env
# All Next.js env vars are provided via .env.production (created by CI)
# and loaded automatically by Next.js during build.
# Only VERSION is needed as a Docker ARG for package.json patching.
ARG VERSION=unversioned
RUN apk add --no-cache jq && \
mv package.json package.json.bak && \
jq --arg version "$VERSION" '.version=$version' package.json.bak > package.json && \
rm package.json.bak && \
apk del jq
# Add dev deps
COPY --from=deps /app/node_modules ./node_modules
COPY . .
# Remove .env.local so .env.production (created by CI) takes effect
RUN rm -f .env.local .env.local.example
RUN yarn build && \
yarn sitemap
# Build target production #
###########################
FROM base AS runner
# Upgrade all system packages to pick up security patches (musl, zlib, openssl, etc.)
# No Docker layer cache is used in CI, so this always fetches the latest on each build.
RUN apk --no-cache upgrade && apk --no-cache add curl
RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 nextjs
# Harden file permissions flagged by Mondoo/CIS benchmarks
# Ensure root group is empty (remove any default members)
RUN delgroup root root && \
cp /etc/group /etc/group- && \
chmod 600 /etc/group- && \
[ -f /etc/passwd- ] && chmod 600 /etc/passwd- || true
# Copy standalone server and its dependencies
COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
# Copy static assets to standalone directory
COPY --from=builder --chown=nextjs:nodejs /app/public ./public
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static
USER nextjs
ENV PORT=3040
EXPOSE 3040
CMD [ "node", "server.js" ]
HEALTHCHECK --interval=5s --timeout=3s --retries=3 CMD curl --fail http://localhost:3040 || exit 1