build(backend): add multi-stage Dockerfile + health HTTP-status coupling (Closes #6)#84
Merged
snowrugar-beep merged 1 commit intoJun 23, 2026
Conversation
Closes VertexChainLabs#6 (the CI pipeline references a docker/backend.Dockerfile that did not exist) and follows up on VertexChainLabs#78 (closed) by honoring the new /health compression-skip path with a coupled HTTP-status / body.status contract that liveness probes can actually observe. New: - docker/backend.Dockerfile: 6-stage build (base, deps, prod-deps, build, test, production) on node:20-alpine. USER node (UID 1000), HEALTHCHECK via wget --spider against /health, --enable-source-maps so production stack traces map back to TypeScript, no system openssl (Node 20 ships bundled), WORKDIR pre-owned so the runtime node user can write cwd. Test stage relies on the existing CI gate inside Backend/src/gists/gist.repository.spec.ts (verified the only Backend/src/**/*spec.ts that touches live DataSource/pg/Pool/TypeORM). - Backend/.dockerignore: matches the CI build context (./Backend). - docker/README.md: target table, design decisions, explicit headline that the build context must be `./Backend` (root-context acceptance commands from the issue cannot copy package.json correctly). Modified: - infrastructure/ci/docker-build-pipeline.yml: corrected `file:` paths to `docker/backend.Dockerfile`; `docker/build-push-action` resolves `file:` relative to the workspace root, not the build context. - Backend/src/health/health.controller.ts: HTTP status now couples to body.status (200 ok / 503 degraded) via @res({ passthrough: true }) so Docker HEALTHCHECK and Kubernetes liveness probes flip to failing when a backing service errors out instead of silently reporting green. - Backend/test/app.e2e-spec.ts, Backend/test/gists.e2e.spec.ts: tolerate either 200 or 503 in the unit sandbox (no live Postgres provisioned) and assert the body envelope invariants in both branches. Tests: - Backend/src/health/health.controller.spec.ts: 6 new Jest unit tests covering the HTTP-status / body.status contract (success, DB failure, PostGIS failure, envelope hygiene). - tsc --noEmit: clean. - eslint on src/{,**}/**/*.ts: clean. - prettier --check: clean. Refs: VertexChainLabs#6, VertexChainLabs#78
Contributor
|
Great work @gbengaeben! 🎉 Multi-stage Dockerfile, |
5 tasks
9 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Closes #6 (the CI pipeline references
docker/backend.Dockerfilethat did not exist) and follows up on closed #78 by giving the compression-skip/healthendpoint a coupled HTTP-status / body.status contract that liveness probes can actually observe.Files
New (3):
docker/backend.Dockerfile— 6-stage build onnode:20-alpine.USER node(UID 1000), HEALTHCHECK viawget --spideragainst/health,--enable-source-mapsso production stack traces map back to TypeScript.apk adddrops systemopenssl(Node 20 ships bundled) and pre-chowns the WORKDIR so the runtime node user can write cwd. Theteststage relies on the existing CI gate insideBackend/src/gists/gist.repository.spec.ts:12.Backend/.dockerignore— matches the CI build context (./Backend).docker/README.md— target table, design decisions, explicit headline that the build context must be./Backend(root-context acceptance commands from the issue cannot satisfyCOPY package.json).Modified (5):
infrastructure/ci/docker-build-pipeline.yml—file:paths corrected todocker/backend.Dockerfile.docker/build-push-actionresolvesfile:relative to the workspace root, not the build context, so no..traversal is needed.Backend/src/health/health.controller.ts— HTTP status now couples tobody.status(200 ok / 503 degraded) via@Res({ passthrough: true })so Docker HEALTHCHECK and Kubernetes liveness probes flip to failing when a backing service errors out instead of silently reporting green.Backend/src/health/health.controller.spec.ts(new) — 6 Jest unit tests covering the contract.Backend/test/app.e2e-spec.tsandBackend/test/gists.e2e.spec.ts— tolerate 200/503 in the unit sandbox (no live Postgres provisioned) and assert envelope invariants in both branches.Acceptance criteria mapping (from issue #6)
docker build --target build -f docker/backend.Dockerfile <context>succeedsbuildstage inheritsdepsand runsnest build→/usr/src/app/distdocker build --target test -f docker/backend.Dockerfile <context>succeeds and runs teststeststage setsENV CI=truewhich activates the existingdescribe.skipgate atBackend/src/gists/gist.repository.spec.ts:12; runsnpm test -- --coverage --testPathIgnorePatterns='\.e2e-spec\.ts$'docker build --target production -f docker/backend.Dockerfile <context>produces <200 MB imageproductionstage onnode:20-alpine+--omit=devnode_modules+ compileddist; no source maps, no dev tooling, no.gitENV PORT=3000;CMD ["node", "--enable-source-maps", "dist/main"]; NestJS listens onprocess.env.PORT ?? 3000USER node(UID 1000, ships withnode:20-alpine)docker-build-pipeline.ymlpasses with the new Dockerfilefile:paths fixed; build/test/security-scan/push jobs all point atdocker/backend.DockerfileARG NODE_VERSION=20,FROM node:${NODE_VERSION}-alpine AS basewget, which lets us use the standardHEALTHCHECKdirective without vouching for a Node-based liveness script)HEALTHCHECK --interval=30s --timeout=5s --start-period=30s --retries=3 CMD wget --quiet --tries=1 --spider http://127.0.0.1:${PORT}/health || exit 1Deviations from the literal acceptance commands
The issue uses
docker build ... .(repo-root context). That invocation would not work with this Dockerfile becauseCOPY package.json package-lock.json* ./resolves against the context root —package.jsonlives atBackend/package.json, not at the repo root. The Dockerfile is therefore built with./Backendas the context to match how the CI pipeline posts it (context: ./Backendindocker/build-push-action@v5).docker/README.mdmakes this constraint explicit and lists every verification command with the correct context.Compression-middleware compatibility (refs #78)
Backend/src/common/middleware/compression.middleware.tsalready lists/healthinDEFAULT_SKIP_PATHSand skipsHEAD/OPTIONSrequests. Now that/healthreturns HTTP 503 when degraded, the new contract is observable by every consumer (Docker HEALTHCHECK, k8s liveness probe, ALB target group) without an additional debug endpoint. Body shape is preserved exactly so existing JSON scrapers parse unchanged.Verification
npx tsc --noEmit— cleannpx eslint "{src,test}/**/*.ts"— cleannpx prettier --check— cleannpx jest src/health— 6/6 passlint-staged) — passes (this commit went through it)docker/README.md.Risk
Low. Isolated to a new infra asset (CI-time builds + k8s image). No application code paths change; the only change to a controller is the HTTP-status coupling for
/health, which behaves identically to before when both PostGIS and Postgres probes pass.Out of scope / follow-ups
/api/docsat runtime in production (SwaggerModule.setup is unconditional).COPY tsconfig.jsonis potentially unused ifnest buildonly honorstsconfig.build.jsonvianest-cli.json.Refs #6, #78