Skip to content

Commit 1903608

Browse files
authored
Merge pull request #2 from StackExchange/container-hosting
Add production container image, health endpoint and CI
2 parents 487f7c6 + 17513ec commit 1903608

7 files changed

Lines changed: 180 additions & 1 deletion

File tree

‎.dockerignore‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
.git
2+
.github
3+
.next
4+
node_modules
5+
.pnpm-store
6+
dist
7+
coverage
8+
test-results
9+
playwright-report
10+
*.tsbuildinfo
11+
**/.env*
12+
.DS_Store
13+
14+
# Agent tooling and design docs are not part of the runtime image.
15+
.agents
16+
.claude
17+
.codex
18+
.impeccable
19+
.superpowers
20+
.worktrees
21+
docs
22+
e2e
23+
Dockerfile
24+
.dockerignore

‎.github/workflows/ci.yml‎

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
name: CI
2+
3+
on:
4+
pull_request:
5+
push:
6+
branches: [main]
7+
8+
permissions:
9+
contents: read
10+
11+
concurrency:
12+
group: ci-${{ github.ref }}
13+
cancel-in-progress: true
14+
15+
jobs:
16+
test:
17+
name: Lint, test and build
18+
runs-on: ubuntu-latest
19+
steps:
20+
- uses: actions/checkout@v4
21+
- uses: pnpm/action-setup@v4
22+
- uses: actions/setup-node@v4
23+
with:
24+
node-version: 24
25+
cache: pnpm
26+
- run: pnpm install --frozen-lockfile
27+
- run: pnpm lint
28+
- run: pnpm test
29+
- run: pnpm build
30+
31+
container:
32+
name: Container image
33+
runs-on: ubuntu-latest
34+
steps:
35+
- uses: actions/checkout@v4
36+
- uses: docker/setup-buildx-action@v3
37+
- name: Build image
38+
uses: docker/build-push-action@v6
39+
with:
40+
context: .
41+
load: true
42+
tags: stack-api-utilities:ci
43+
- name: Smoke-test hardened container
44+
run: |
45+
set -euo pipefail
46+
docker run -d --name smoke -p 3000:3000 \
47+
--read-only --tmpfs /tmp --tmpfs /app/.next/cache:uid=10001,gid=10001 \
48+
--cap-drop ALL --security-opt no-new-privileges \
49+
stack-api-utilities:ci
50+
for _ in $(seq 1 30); do
51+
curl -sf http://127.0.0.1:3000/api/healthz && exit 0
52+
sleep 1
53+
done
54+
docker logs smoke
55+
exit 1

‎Dockerfile‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# syntax=docker/dockerfile:1.7
2+
#
3+
# Production image for Stack API Utilities (Next.js standalone server).
4+
#
5+
# docker build -t stack-api-utilities .
6+
# docker run --rm -p 3000:3000 \
7+
# -e STACK_API_UTILITIES_PUBLIC_ORIGIN=http://localhost:3000 stack-api-utilities
8+
#
9+
# The server is stateless: customer credentials and datasets stay in the
10+
# browser, and OAuth PKCE state travels in a short-lived cookie. The image
11+
# therefore needs no secrets and no persistent volume.
12+
13+
ARG NODE_VERSION=24
14+
15+
FROM node:${NODE_VERSION}-bookworm-slim AS base
16+
ENV PNPM_HOME=/pnpm \
17+
PATH=/pnpm:$PATH \
18+
NEXT_TELEMETRY_DISABLED=1
19+
RUN corepack enable
20+
WORKDIR /app
21+
22+
FROM base AS deps
23+
COPY package.json pnpm-lock.yaml pnpm-workspace.yaml ./
24+
RUN --mount=type=cache,id=pnpm-store,target=/pnpm/store \
25+
pnpm config set store-dir /pnpm/store && \
26+
pnpm install --frozen-lockfile
27+
28+
FROM base AS build
29+
COPY --from=deps /app/node_modules ./node_modules
30+
COPY . .
31+
RUN pnpm build
32+
33+
FROM node:${NODE_VERSION}-bookworm-slim AS runtime
34+
ARG APP_VERSION=0.0.0.0
35+
ENV NODE_ENV=production \
36+
NEXT_TELEMETRY_DISABLED=1 \
37+
HOSTNAME=0.0.0.0 \
38+
PORT=3000 \
39+
APP_VERSION=${APP_VERSION}
40+
WORKDIR /app
41+
42+
RUN groupadd --system --gid 10001 app && \
43+
useradd --system --uid 10001 --gid app --no-create-home app
44+
45+
COPY --from=build --chown=10001:10001 /app/.next/standalone ./
46+
COPY --from=build --chown=10001:10001 /app/.next/static ./.next/static
47+
48+
USER 10001:10001
49+
EXPOSE 3000
50+
51+
HEALTHCHECK --interval=30s --timeout=3s --start-period=10s --retries=3 \
52+
CMD ["node", "-e", "fetch('http://127.0.0.1:'+(process.env.PORT||3000)+'/api/healthz').then(r=>process.exit(r.ok?0:1)).catch(()=>process.exit(1))"]
53+
54+
CMD ["node", "server.js"]

‎README.md‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,22 @@ Run the production server:
347347
pnpm preview
348348
```
349349

350+
## Container Image
351+
352+
The repository ships a production `Dockerfile` that builds the Next.js standalone server on Node 24 and runs it as a non-root user (uid 10001) on port 3000.
353+
354+
```bash
355+
docker build -t stack-api-utilities .
356+
docker run --rm -p 3000:3000 \
357+
-e STACK_API_UTILITIES_PUBLIC_ORIGIN=http://localhost:3000 \
358+
stack-api-utilities
359+
```
360+
361+
- The server is stateless. Credentials and datasets stay in the browser, and OAuth PKCE state travels in a short-lived cookie, so the container needs no secrets or volumes.
362+
- Set `STACK_API_UTILITIES_PUBLIC_ORIGIN` to the public HTTPS origin when the app runs behind a TLS-terminating proxy, so the OAuth callback URL is correct.
363+
- `GET /api/healthz` returns `{"ok":true}` for liveness and readiness probes.
364+
- The image runs with a read-only root filesystem if `/tmp` and `/app/.next/cache` are writable mounts.
365+
350366
## License
351367

352368
Released under the [MIT License](LICENSE).

‎next.config.mjs‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
/** @type {import('next').NextConfig} */
2-
const nextConfig = {};
2+
const nextConfig = {
3+
// Emit a self-contained server (.next/standalone) so the container image
4+
// ships only the traced runtime files instead of the full node_modules tree.
5+
output: "standalone",
6+
};
37

48
export default nextConfig;

‎src/app/api/healthz/route.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { NextResponse } from "next/server";
2+
3+
export const runtime = "nodejs";
4+
export const dynamic = "force-dynamic";
5+
6+
// Liveness/readiness probe for container hosting. It only proves the Node
7+
// process is serving requests; it deliberately calls no upstream Stack API.
8+
export async function GET(): Promise<NextResponse> {
9+
return NextResponse.json({ ok: true }, { headers: { "cache-control": "no-store" } });
10+
}

‎src/server/healthzRoute.test.ts‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { describe, expect, it } from "vitest";
2+
import { GET as handleHealthzGet, dynamic } from "../app/api/healthz/route";
3+
4+
describe("GET /api/healthz", () => {
5+
it("reports the process as healthy without caching", async () => {
6+
const response = await handleHealthzGet();
7+
8+
expect(response.status).toBe(200);
9+
expect(response.headers.get("cache-control")).toBe("no-store");
10+
await expect(response.json()).resolves.toEqual({ ok: true });
11+
});
12+
13+
it("is never statically prerendered", () => {
14+
expect(dynamic).toBe("force-dynamic");
15+
});
16+
});

0 commit comments

Comments
 (0)