Repository navigation
Expand file tree
/
Copy pathDockerfile
More file actions
140 lines (112 loc) · 5.69 KB
/
Copy pathDockerfile
File metadata and controls
140 lines (112 loc) · 5.69 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# Stage 1: Dependencies
FROM node:lts-alpine AS deps
RUN apk add --no-cache libc6-compat
WORKDIR /app
# Keep patches available before npm ci: patch-package skips missing patches during postinstall.
COPY package.json package-lock.json ./
COPY patches ./patches
# Install all dependencies (including devDependencies for build).
# The Cypress binary (~640 MB unpacked) is only used by the e2e runner, never by the
# image build, so its postinstall download is skipped; npm's download cache is dropped
# in the same layer. Neither reaches the final image, but both made this layer heavy.
RUN CYPRESS_INSTALL_BINARY=0 npm ci && npm cache clean --force
# Stage 2: Builder
FROM node:lts-alpine AS builder
WORKDIR /app
# Copy dependencies from deps stage
COPY --from=deps /app/node_modules ./node_modules
COPY . .
# Declare build arguments for Next.js public env vars (baked at build time)
# All have defaults for simplicity - can be overridden if needed
ARG NEXT_PUBLIC_DB_VERSION
ARG NEXT_PUBLIC_DB_NAME
ARG NEXT_PUBLIC_DEBUG_MODE
ARG NEXT_PUBLIC_APP_VERSION
# NOTE: NEXUS_URL, CDN_URL, HOMESERVER, HOMESERVER_URL, HOMEGATE_URL, DEFAULT_HTTP_RELAY,
# PKARR_RELAYS and TESTNET are intentionally NOT build args. They are runtime-configurable and must
# be supplied as PUBKY_RUNTIME_* environment variables on the running container (see runner stage
# and src/libs/runtime-config). This lets a single image be promoted across staging/prod/testnet.
# NOTE: Sentry runtime values (DSN, environment, sample rates) are runtime-configurable
# (PUBKY_RUNTIME_SENTRY_*, see runner stage). Source-map upload is optional: Synonym CI passes
# Sentry build credentials, while third-party public image builds skip upload and still succeed.
# Disable telemetry during build
ENV NEXT_TELEMETRY_DISABLED=1
# Enable standalone output for Docker builds
ENV NEXT_STANDALONE=true
# `next build --webpack` exceeds Node's default ~2GB heap inside this image
# (pubky-stack CI: JavaScript heap out of memory, then SIGABRT). Raise the
# old-space limit for the build. The runner stage does not inherit this ENV.
ENV NODE_OPTIONS=--max-old-space-size=4096
# Build the application
RUN npm run build
# Inject Sentry Debug IDs into the built chunks and their source maps (deterministic, offline,
# no credentials). The shipped JS must carry the same Debug IDs as the maps the CI pipeline
# uploads — that pipeline extracts the maps from THIS stage (docker build --target builder),
# so it must not re-inject. Browser maps are stripped from the runner stage below.
# The standalone tree needs its own pass: sentry-cli skips hidden directories while walking,
# so the nested .next/standalone/.next is missed by the first command. IDs are derived from
# file content, so the standalone copies receive the exact same IDs as the originals.
RUN npx sentry-cli sourcemaps inject .next \
&& npx sentry-cli sourcemaps inject .next/standalone/.next
ARG SENTRY_AUTH_TOKEN
ARG SENTRY_ORG
ARG SENTRY_PROJECT
RUN if [ -n "$SENTRY_AUTH_TOKEN" ] && [ -n "$SENTRY_ORG" ] && [ -n "$SENTRY_PROJECT" ]; then \
npx sentry-cli sourcemaps upload --release="$NEXT_PUBLIC_APP_VERSION" .next; \
else \
echo "Skipping Sentry source-map upload; Sentry build credentials not set."; \
fi
# Strip browser source maps from the public image: the chunks keep their injected Debug IDs
# (enough for Sentry to match the maps uploaded by the CI pipeline), and the maps themselves
# must not be served from /_next/static. Server-side maps under .next/standalone stay — they
# are never exposed over HTTP and make Node stack traces readable.
RUN find .next/static -name '*.map' -type f -delete
# Stage 3: Runner
FROM node:lts-alpine AS runner
WORKDIR /app
# Set production environment
ENV NODE_ENV=production
ENV NEXT_TELEMETRY_DISABLED=1
# Create non-root user for security
RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 nextjs
# Copy public assets
COPY --from=builder --chown=nextjs:nodejs /app/public ./public
# Set correct permissions for prerender cache
RUN mkdir .next
RUN chown nextjs:nodejs .next
# 1. Copy standalone server files (Next.js automatically links to .next/static from here)
COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
# 2. Copy the client-side static assets from the root .next/static folder
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static
# Switch to non-root user
USER nextjs
# Expose the default Next.js port
EXPOSE 3000
ENV PORT=3000
ENV HOSTNAME="0.0.0.0"
# Runtime configuration (PUBLIC values, NOT secrets) is supplied per-environment at container
# runtime (Ansible / docker compose / k8s). With NODE_ENV=production the app fails fast (at boot)
# if any of the REQUIRED network values or the Shop destination are missing rather than silently falling back to staging
# defaults. Optional/defaulted PUBKY_RUNTIME_* values can override deployer-facing public config
# without rebuilding the image. See docs/environment.md and src/libs/runtime-config.
#
# Required:
# PUBKY_RUNTIME_NEXUS_URL
# PUBKY_RUNTIME_CDN_URL
# PUBKY_RUNTIME_HOMESERVER
# PUBKY_RUNTIME_HOMESERVER_URL
# PUBKY_RUNTIME_HOMEGATE_URL
# PUBKY_RUNTIME_DEFAULT_HTTP_RELAY
# PUBKY_RUNTIME_PKARR_RELAYS (JSON array string, e.g. '["https://pkarr.pubky.app"]')
# PUBKY_RUNTIME_TESTNET ("true" | "false")
# PUBKY_RUNTIME_ENV ("production" | "staging"; drives the staging sign-in guard)
#
# Optional (absent DSN disables Sentry entirely; rates have defaults 0.1 / 0.0 / 1.0):
# PUBKY_RUNTIME_SENTRY_DSN
# PUBKY_RUNTIME_SENTRY_ENVIRONMENT
# PUBKY_RUNTIME_SENTRY_TRACES_SAMPLE_RATE
# PUBKY_RUNTIME_SENTRY_REPLAYS_SESSION_SAMPLE_RATE
# PUBKY_RUNTIME_SENTRY_REPLAYS_ON_ERROR_SAMPLE_RATE
# Run the standalone server
CMD ["node", "server.js"]