ADR-002: One multi-stage Docker image serves the API and the SPA
Date: 2026-08-31
Status: Accepted
In plain words
The site ships as one container image (Docker) that runs both the server code and the web pages. The image is built in stages, so the final one carries only the runtime and runs as a user without admin rights (non-root), and its health check asks the app a real question.
What that is worth: a developer runs the whole site with one command and can explain every line of the build, and the organization ships an image with no build tools in it, which leaves less to attack.
Context
The application is a .NET 10 minimal API plus a Vite React SPA. Development
happens on Windows; the container must run on Linux hosts. The Dockerfile is a
portfolio artifact: every line has to be explainable in an interview.
Decision
A single image built in three stages:
- node:22-alpine builds the SPA with
npm ci (deterministic, lockfile-driven),
manifests copied before source so dependency restore caches independently.
- dotnet/sdk:10.0 publishes the API in Release. The TargetFramework is read
out of the csproj at build time rather than hard-coded, and the build fails
loudly if it cannot be resolved.
- dotnet/aspnet:10.0 is the final runtime stage: no SDK, no compilers. The API
serves the SPA (static files plus MapFallbackToFile("index.html") mapped
after the API routes, so deep links work and /api is never swallowed).
Runtime facts: port 8080 via ASPNETCORE_URLS and EXPOSE; a HEALTHCHECK curls
/api/facets so health means "answering real traffic", not "process exists";
the container runs as the aspnet image's built-in non-root app user, with
file ownership set per-COPY via --chown instead of a duplicate chown layer.
README.md, docs/ and data/ are copied into the image because the app serves
its documentation from the About menu and loads the dataset at runtime.
A .dockerignore keeps node_modules, bin, obj and .git out of the build context.
Alternatives considered
- Two containers (nginx for the SPA, the API behind it) with compose or an
ingress. The standard microservice shape, rejected here: one process to
run, one origin to front, zero CORS surface, and the API already owns
static serving. Right answer at larger scale, unnecessary overhead at this
one.
- Shipping the SDK image as the final stage. Rejected: size and attack
surface; the runtime image carries no toolchain.
- Creating a custom non-root user in the Dockerfile. Rejected after it
collided with the base image: aspnet ships a built-in
app user (APP_UID)
for exactly this purpose, and using it is the current best practice.
Consequences
- Roughly 380 MB on disk, of which the application layers are about 17 MB on
top of Microsoft's runtime image.
- Layer caching behaves predictably: editing one C# file rebuilds only the
publish and final-stage copies; editing package.json rebuilds only the npm
layers.
- Visitors run the whole thing with
npm run docker and stop it with
npm run docker:stop.
Where it sits
No ring applies here: the record is a packaging decision about how the Dockerfile builds one image for the API and the SPA, and SOLID has nothing to say about it.
Files
The frontend build stage:
FROM mirror.gcr.io/library/node:24-alpine AS frontend-build
WORKDIR /src
COPY package*.json ./
RUN npm ci
COPY index.html ./
COPY tsconfig.json ./
COPY tsconfig.app.json ./
COPY tsconfig.node.json ./
COPY vite.config.ts ./
COPY src ./src
COPY data/recommendations.json ./data/recommendations.json
COPY data/hooks.json ./data/hooks.json
COPY data/quality-history.json ./data/quality-history.json
COPY data/verified.json ./data/verified.json
COPY docs/CHANGELOG.md ./docs/CHANGELOG.md
COPY data/coderabbit-findings.json ./data/coderabbit-findings.json
COPY data/test-results.json ./data/test-results.json
COPY data/gate-reds.json ./data/gate-reds.json
COPY public ./public
RUN npm run build
ARG APP_VERSION=dev
ARG GOOGLE_SITE_VERIFICATION=
RUN set -e; \
sed -i "s/__BUILD_DATE__/$(date -u +%Y-%m-%d)/" dist/sitemap.xml; \
if grep -q "__BUILD_DATE__" dist/sitemap.xml; then echo "the sitemap kept its placeholder" >&2; exit 1; fi; \
if [ -n "${GOOGLE_SITE_VERIFICATION}" ]; then \
case "${GOOGLE_SITE_VERIFICATION}" in *[!A-Za-z0-9_-]*) echo "GOOGLE_SITE_VERIFICATION holds a character no token has" >&2; exit 1;; esac; \
sed -i "s|<!-- google-site-verification -->|<meta name=\"google-site-verification\" content=\"${GOOGLE_SITE_VERIFICATION}\" />|" dist/index.html; \
grep -q "name=\"google-site-verification\"" dist/index.html; \
fi; \
echo "stamped: $(grep -o '<lastmod>[^<]*</lastmod>' dist/sitemap.xml) for ${APP_VERSION}"
Live from Dockerfile, region frontend-build, read from this build at 99df7a8.
The API publish stage:
FROM mcr.microsoft.com/dotnet/sdk:11.0.100 AS api-publish
WORKDIR /src
COPY api/TheYard.slnx ./api/
COPY api/TheYard.Data/TheYard.Data.csproj ./api/TheYard.Data/
COPY api/TheYard.Domain/TheYard.Domain.csproj ./api/TheYard.Domain/
COPY api/TheYard.Application/TheYard.Application.csproj ./api/TheYard.Application/
COPY api/TheYard.Infrastructure/TheYard.Infrastructure.csproj ./api/TheYard.Infrastructure/
COPY api/TheYard.Infrastructure.Cosmos/TheYard.Infrastructure.Cosmos.csproj ./api/TheYard.Infrastructure.Cosmos/
COPY api/TheYard.Experiment/TheYard.Experiment.csproj ./api/TheYard.Experiment/
COPY api/TheYard.Migrations.Sqlite/TheYard.Migrations.Sqlite.csproj ./api/TheYard.Migrations.Sqlite/
COPY api/TheYard.Database/TheYard.Database.sqlproj ./api/TheYard.Database/
COPY api/TheYard.Api/TheYard.Api.csproj ./api/TheYard.Api/
COPY api/TheYard.Tests/TheYard.Tests.csproj ./api/TheYard.Tests/
COPY samples/theshed/TestProject.csproj ./samples/theshed/
RUN dotnet restore api/TheYard.slnx
RUN dotnet restore api/TheYard.Api/TheYard.Api.csproj -r linux-x64 -p:PublishReadyToRun=true
COPY api ./api
COPY samples/theshed ./samples/theshed
RUN set -eu; \
TARGET_FRAMEWORK="$(grep -R -h -m1 --include='*.csproj' -Eo '<TargetFramework>[^<]+</TargetFramework>' /src/api | sed -E 's#<TargetFramework>([^<]+)</TargetFramework>#\1#' | head -n 1)"; \
[ -n "${TARGET_FRAMEWORK}" ] || { echo "No TargetFramework found under /src/api" >&2; exit 1; }; \
echo "Using target framework: ${TARGET_FRAMEWORK}"; \
dotnet publish api/TheYard.Api/TheYard.Api.csproj -c Release --no-restore -f "${TARGET_FRAMEWORK}" \
-r linux-x64 --self-contained false -p:PublishReadyToRun=true -o /app/publish; \
dotnet publish samples/theshed/TestProject.csproj -c Release -o /app/shed
Live from Dockerfile, region api-publish, read from this build at 99df7a8.
The runtime stage, non-root, with the health check and the sources the live
samples read (ADR: Live code samples):
FROM mcr.microsoft.com/dotnet/aspnet:11.0 AS runtime
WORKDIR /app
RUN apt-get update && apt-get install -y --no-install-recommends curl && rm -rf /var/lib/apt/lists/*
COPY --chown=app:app --from=api-publish /app/publish/ /app/
COPY --chown=app:app README.md ./
COPY --chown=app:app docs ./docs
COPY --chown=app:app data ./data
COPY --chown=app:app infra ./infra
COPY --chown=app:app src ./src
COPY --chown=app:app .github/workflows ./.github/workflows
COPY --chown=app:app .github/quality.py ./.github/
COPY --chown=app:app .github/quality-ratchet.json ./.github/
COPY --chown=app:app .coderabbit.yaml ./
COPY --chown=app:app .vscode ./.vscode
COPY --chown=app:app api/TheYard.slnx ./api/
COPY --chown=app:app api/TheYard.Api/*.cs api/TheYard.Api/*.csproj ./api/TheYard.Api/
COPY --chown=app:app api/TheYard.Api/Shared ./api/TheYard.Api/Shared
COPY --chown=app:app api/TheYard.Api/Features ./api/TheYard.Api/Features
COPY --chown=app:app api/TheYard.Application/*.csproj ./api/TheYard.Application/
COPY --chown=app:app api/TheYard.Application/Features ./api/TheYard.Application/Features
COPY --chown=app:app api/TheYard.Application/Shared ./api/TheYard.Application/Shared
COPY --chown=app:app api/TheYard.Data/*.cs api/TheYard.Data/*.csproj ./api/TheYard.Data/
COPY --chown=app:app api/TheYard.Domain/*.csproj ./api/TheYard.Domain/
COPY --chown=app:app api/TheYard.Domain/Features ./api/TheYard.Domain/Features
COPY --chown=app:app api/TheYard.Domain/Shared ./api/TheYard.Domain/Shared
COPY --chown=app:app api/TheYard.Infrastructure/*.cs api/TheYard.Infrastructure/*.csproj ./api/TheYard.Infrastructure/
COPY --chown=app:app api/TheYard.Infrastructure/Features ./api/TheYard.Infrastructure/Features
COPY --chown=app:app api/TheYard.Infrastructure/Shared ./api/TheYard.Infrastructure/Shared
COPY --chown=app:app api/TheYard.Infrastructure.Cosmos/*.csproj ./api/TheYard.Infrastructure.Cosmos/
COPY --chown=app:app api/TheYard.Infrastructure.Cosmos/Features ./api/TheYard.Infrastructure.Cosmos/Features
COPY --chown=app:app api/TheYard.Infrastructure.Cosmos/Shared ./api/TheYard.Infrastructure.Cosmos/Shared
COPY --chown=app:app api/TheYard.Experiment/*.cs api/TheYard.Experiment/*.csproj ./api/TheYard.Experiment/
COPY --chown=app:app api/TheYard.Migrations.Sqlite/*.cs api/TheYard.Migrations.Sqlite/*.csproj ./api/TheYard.Migrations.Sqlite/
COPY --chown=app:app functions/jobhunt/*.cs ./functions/jobhunt/
COPY --chown=app:app functions/jobhunt.Tests ./functions/jobhunt.Tests
COPY --chown=app:app server/JobHunt.Server.Application/Features/Archive ./server/JobHunt.Server.Application/Features/Archive
COPY --chown=app:app api/TheYard.Database ./api/TheYard.Database
COPY --chown=app:app api/TheYard.Tests/*.cs api/TheYard.Tests/*.csproj ./api/TheYard.Tests/
COPY --chown=app:app Dockerfile netlify.toml playwright.config.ts vite.config.ts package.json index.html tsconfig.json tsconfig.app.json tsconfig.node.json .editorconfig ./
COPY --chown=app:app edge ./edge
COPY --chown=app:app tests ./tests
COPY --chown=app:app scripts ./scripts
COPY --chown=app:app render ./render
COPY --chown=app:app samples/theshed/tests/TestProject.Tests/OnionTests.cs ./samples/theshed/tests/TestProject.Tests/
COPY --chown=app:app --from=api-publish /app/shed/ /app/shed/
COPY --chown=app:app --from=frontend-build /src/dist/ /app/wwwroot/
ARG APP_VERSION=dev
ARG APP_COMMIT=local
ENV APP_VERSION=${APP_VERSION}
ENV APP_COMMIT=${APP_COMMIT}
ENV SHED_COMMIT=${APP_COMMIT}
ENV Shed__ContentRoot=/app/shed
ENV ASPNETCORE_URLS=http://+:8080
EXPOSE 8080
HEALTHCHECK --interval=30s --timeout=5s --start-period=20s --retries=3 CMD curl -fsS http://localhost:8080/healthz || exit 1
RUN mkdir -p /app/state && chown app:app /app/state
ENV ConnectionStrings__Yard="Data Source=/app/state/yard.db"
USER app
ENTRYPOINT ["dotnet", "TheYard.Api.dll"]
Live from Dockerfile, region runtime, read from this build at 99df7a8.
Addendum, 2026-10-03: the build stage is Node 24
The build stage moved from node:22-alpine to node:24-alpine, and CI from Node 22 to Node 24, because 22 is in maintenance and 24 is the active long-term release (ADR: Technology versions). Nothing else in the stage changed.
Addendum, 2026-10-09 (1.0.3.109): the node image comes from a mirror that needs no account
Deploy #292 for 1.0.3.108 failed in two seconds at Build and push, and twice more when re-run: Docker Hub answered the metadata request for node:24-alpine from GitHub's runner with 429 Too Many Requests, and once with a 504 from its token service. Nothing in the commit; the anonymous pull allowance is shared by every runner on the address, and the two .NET images come from Microsoft's registry, which has no such limit. The three Node stages, the site's build stage and the rendering service's build and run stages, now pull the same official image from AWS's public mirror of it, public.ecr.aws/docker/library/node:24-alpine, which Docker publishes there itself, needs no account and no secret, and is not metered that way. Read from the build machine before the change: the mirror answers the tag with the image index, status 200. A signed-in pull from Docker Hub would have raised the allowance instead, at the price of a Docker account and a secret in the pipeline, which the rules refuse. TechnologyVersionsTests holds both Dockerfiles to the mirror's address and the Node version, and DockerBuildInputsTests finds the frontend stage by it.
Addendum, 2026-10-15 (1.0.3.135): the second mirror
On 15 October Deploy Render and Deploy each failed twice, every time in under thirty seconds at Build and push: AWS's public mirror answered the manifest request for node:24-alpine with 429 Too Many Requests, "Data limit exceeded". That mirror has an allowance per address for anonymous pulls, and GitHub's runners share their addresses, so a quiet repository can find it already spent. A re-run went through each time, which is luck, not a fix. The three Node stages now pull the same image from Google's mirror of Docker Hub, mirror.gcr.io/library/node:24-alpine, which also needs no account and no secret. Read before the change: the mirror answers the tag with the image index, status 200, digest sha256:44fce696..., the same digest AWS's mirror served, so the bytes in the build do not change. The .NET stages stay on Microsoft's registry. TechnologyVersionsTests holds both Dockerfiles to the new address and DockerBuildInputsTests finds the frontend stage by it.
Enforced by
Not machine-checked. Held by review.