forked from rajnandan1/kener
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDockerfile
More file actions
194 lines (155 loc) · 6.54 KB
/
Dockerfile
File metadata and controls
194 lines (155 loc) · 6.54 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# syntax=docker/dockerfile:1
# =============================================================================
# Kener v4 — Status Page Application
# Multi-stage, multi-variant (Alpine / Debian) Dockerfile
#
# Build:
# docker build -t kener . # Alpine (default)
# docker build -t kener --build-arg VARIANT=debian . # Debian Slim
# docker build -t kener --build-arg WITH_DOCS=true . # Include docs
#
# Run:
# docker run -d -p 3000:3000 \
# -e KENER_SECRET_KEY=<secret> \
# -e ORIGIN=http://localhost:3000 \
# -e REDIS_URL=redis://<host>:6379 \
# -v kener_db:/app/database \
# kener
# =============================================================================
ARG NODE_VERSION=24
ARG VARIANT=alpine
ARG WITH_DOCS=false
ARG KENER_BASE_PATH=
# =============================================================================
# STAGE 1 — BUILDER (installs deps, compiles native modules, builds app)
# =============================================================================
# ---------- Alpine builder ----------
FROM node:${NODE_VERSION}-alpine AS builder-alpine
RUN apk add --no-cache \
build-base \
python3 \
sqlite \
sqlite-dev \
tzdata
# ---------- Debian builder ----------
FROM node:${NODE_VERSION}-slim AS builder-debian
RUN apt-get update && apt-get install -y --no-install-recommends \
build-essential \
python3 \
sqlite3 \
libsqlite3-dev \
tzdata && \
rm -rf /var/lib/apt/lists/*
# ---------- Selected variant ----------
FROM builder-${VARIANT} AS builder
ENV NPM_CONFIG_LOGLEVEL=error
WORKDIR /app
ARG KENER_BASE_PATH
ENV KENER_BASE_PATH=${KENER_BASE_PATH}
# 1. Copy package manifests first (maximises layer cache hits)
COPY package*.json ./
# 2. Install ALL dependencies (devDependencies needed for the build step)
RUN npm ci --no-fund && \
npm cache clean --force
# 3. Copy the rest of the source tree
COPY . .
# 4. Create directories that the app expects
RUN mkdir -p database
# 5. Conditionally remove docs routes before build
# (avoids EXDEV rename error in overlayfs; clean .svelte-kit so stale
# route types don't persist)
ARG WITH_DOCS
RUN if [ "$WITH_DOCS" != "true" ]; then \
rm -rf src/routes/\(docs\) .svelte-kit; \
fi
# 6. Build: SvelteKit (vite) + server bundle (esbuild)
# Use build-with-docs when docs are enabled
RUN if [ "$WITH_DOCS" = "true" ]; then \
npm run build-with-docs; \
else \
npm run build; \
fi
# 7. Stage docs runtime files for index-docs (empty dir when docs disabled)
RUN mkdir -p /docs-runtime && \
if [ "$WITH_DOCS" = "true" ]; then \
mkdir -p /docs-runtime/scripts && \
mkdir -p /docs-runtime/src/lib && \
mkdir -p "/docs-runtime/src/routes/(docs)/docs" && \
cp scripts/index-docs.ts /docs-runtime/scripts/ && \
cp src/lib/marked.ts /docs-runtime/src/lib/ && \
cp "src/routes/(docs)/docs.json" "/docs-runtime/src/routes/(docs)/" && \
cp -r "src/routes/(docs)/docs/content" "/docs-runtime/src/routes/(docs)/docs/"; \
fi
# 8. Remove devDependencies from node_modules
RUN npm prune --omit=dev
# =============================================================================
# STAGE 2 — PRODUCTION (minimal runtime image)
# =============================================================================
# ---------- Alpine runtime ----------
FROM node:${NODE_VERSION}-alpine AS final-alpine
RUN apk add --no-cache \
sqlite \
tzdata \
iputils \
curl \
libcap && \
# Grant ping the NET_RAW capability so non-root users can send ICMP packets
setcap cap_net_raw+ep /bin/ping || true
# ---------- Debian runtime ----------
FROM node:${NODE_VERSION}-slim AS final-debian
RUN apt-get update && apt-get install -y --no-install-recommends \
sqlite3 \
tzdata \
iputils-ping \
curl \
libcap2-bin && \
setcap cap_net_raw+ep /usr/bin/ping || true && \
rm -rf /var/lib/apt/lists/*
# ---------- Selected variant ----------
FROM final-${VARIANT} AS final
ARG PORT=3000
ARG KENER_BASE_PATH=
ENV NODE_ENV=production \
PORT=${PORT} \
KENER_BASE_PATH=${KENER_BASE_PATH} \
BODY_SIZE_LIMIT=3M \
TZ=UTC \
# Required so Node can import .ts migration/seed files at runtime
NODE_OPTIONS="--experimental-strip-types"
WORKDIR /app
# Create writable directories owned by the non-root "node" user
# (node:node is provided by the official Node.js images)
RUN mkdir -p database && \
chown -R node:node /app
# ---- Copy artifacts from builder (order: least → most likely to change) ----
# Production node_modules (largest layer, changes least often)
COPY --chown=node:node --from=builder /app/node_modules ./node_modules
# Package manifest (needed for ESM "type":"module" resolution)
COPY --chown=node:node --from=builder /app/package.json ./package.json
# Knex migrations & seeds (run at startup by build/main.js)
COPY --chown=node:node --from=builder /app/migrations ./migrations
COPY --chown=node:node --from=builder /app/seeds ./seeds
# Seed data files imported by seeds at runtime (all are leaf modules)
COPY --chown=node:node --from=builder /app/src/lib/server/db/seedSiteData.ts ./src/lib/server/db/seedSiteData.ts
COPY --chown=node:node --from=builder /app/src/lib/server/db/seedMonitorData.ts ./src/lib/server/db/seedMonitorData.ts
COPY --chown=node:node --from=builder /app/src/lib/server/db/seedPagesData.ts ./src/lib/server/db/seedPagesData.ts
COPY --chown=node:node --from=builder /app/src/lib/allPerms.ts ./src/lib/allPerms.ts
COPY --chown=node:node --from=builder /app/src/lib/server/templates/general ./src/lib/server/templates/general
# Locale JSON files (read at runtime by server-side i18n)
COPY --chown=node:node --from=builder /app/src/lib/locales ./src/lib/locales
# Build output (SvelteKit client/server + esbuild main.js) — changes most often
COPY --chown=node:node --from=builder /app/build ./build
# Docs runtime files (index-docs script + markdown sources; empty when WITH_DOCS=false)
COPY --chown=node:node --from=builder /docs-runtime/ ./
# Entrypoint script (runs index-docs on startup when docs are bundled)
COPY --chown=node:node docker-entrypoint.sh ./docker-entrypoint.sh
RUN chmod +x docker-entrypoint.sh
# ---- Runtime configuration ----
# Switch to non-root user
USER node
EXPOSE ${PORT}
# Healthcheck: hit the /healthcheck endpoint exposed by Express in main.ts
HEALTHCHECK --interval=30s --timeout=5s --start-period=15s --retries=3 \
CMD sh -c 'curl -sf http://localhost:${PORT}${KENER_BASE_PATH}/healthcheck || exit 1'
ENTRYPOINT ["./docker-entrypoint.sh"]
CMD ["node", "build/main.js"]