Two Scary Bases, Zero Runtimes
A colleague paused on a Dockerfile review last week and said something that
stuck. Two bases this different feels insecure — shouldn’t we split them?
The file pulled node:alpine for the SPA build, golang:alpine for the
backend, and shipped the final image on gcr.io/distroless/static. The
discomfort is honest and worth taking seriously. It is also pointing at a
model of Docker that the multi-stage build quietly retired.
The intuition is fair
A Node runtime and a Go runtime are radically different things. They have different package ecosystems, different CVE cadences, different attack surfaces, and very different operational shapes. If both of them ran in production, the right move would be two images, two pipelines, two lifecycles. Mixing them under one roof would be asking for confusion at best, and a security blind spot at worst.
This is the same intuition that makes teams split a frontend/backend
product into an nginx:alpine image serving the SPA and a separate Go
service handling the API. Sometimes that is the correct shape. The question
is when.
What actually ships
The Dockerfile in question, lightly redrawn:
FROM node:22-alpine AS pnpm-build
# install deps, build the SPA
RUN pnpm build # produces /app/admin/dist
FROM golang:1.23-alpine AS go-build
COPY --from=pnpm-build /app/admin/dist/ ./internal/adminbff/static/dist/
# //go:embed pulls /static/dist into the binary at compile time
RUN go build -o /out/admin
FROM gcr.io/distroless/static
COPY --from=go-build /out/admin /admin
ENTRYPOINT ["/admin"]
Three stages, three bases, and only the third one ever leaves the build host. The first two are intermediate artefacts. They are never tagged, never pushed, never pulled by a runtime, and never scanned by anything that scans production images.
The crossing point between the Node world and the Go world is one line:
COPY --from=pnpm-build. After that line the Node universe ceases to
exist for this image. After the third FROM the Go toolchain ceases to
exist too. What ships is the Go binary plus a CA bundle on
distroless/static, with no shell, no package manager, no apk, and no
libc beyond what was statically linked into the binary at compile time.
What Trivy actually sees
Pointing a scanner at the pushed image yields a manifest that looks something like:
gcr.io/distroless/staticbase layer: CA certificates,/etc/passwd,tzdata. No package database to query, because there are no packages.- One ELF binary, around 14 MB, statically linked.
- No
node, nonpm, nopnpm, no Go toolchain, noapk.
That last bullet is the entire argument. The CVE feed for node:22-alpine
and golang:1.23-alpine is scanning code that does not exist in
production. The feed for nginx:alpine, which is what the FE/BE split
alternative would put in front of the SPA, is scanning code that very much
does. nginx in production is an attack surface. node in a build stage
is not.
Why the single Dockerfile is the conservative call
The single Dockerfile is the right shape here not in spite of the two
heavy bases, but because one of them is engineered to disappear. The
//go:embed directive collapses the SPA into the Go binary at compile
time, which collapses the runtime attack surface to one static binary on a
base with no shell. The cost of mixing node and golang in one file is
paid by the build host, not by anything exposed to the internet.
A smaller, knowable principle hides inside that. The most secure image is the one that contains only the code that runs. Multi-stage builds are the mechanism that makes “build-time machinery” and “runtime contents” two different things. A Dockerfile that uses heavy bases for the build stages and a tiny base for the final stage is honouring that distinction. A Dockerfile that uses one base for everything, runtime included, usually isn’t.
When this pattern doesn’t apply
The single-Dockerfile shape is not universal. A few cases where splitting is still the right call:
- SPAs that need runtime configuration injected past build time, such
as a
window.APP_CONFIGpopulated from environment variables on each container start. Embedding freezes the bundle, so the file-serving layer has to be a real process:nginx, Caddy, or similar. The split is back. - Frontends and backends owned by separate teams with separate release cadences. The image boundary is a deployment boundary, and forcing two release timelines to share one is a coupling cost that usually outweighs the attack-surface win.
- Backends in languages without a clean static-binary story — Python, a Node BFF, a JVM service that needs a JRE. The runtime stage carries the language ecosystem with it, and the FE/BE asymmetry that makes embedding cheap goes away.
The pattern is specifically “frontend artefact plus a statically-linkable backend that can swallow it.” Outside that envelope, two Dockerfiles is the conservative move.
What the worry was actually about
The instinct to split was a good one in disguise. Behind “two scary bases”
is a more useful question: what does the final image actually contain?
Once that question gets answered concretely, by reading the last FROM
and walking through what each COPY --from= line moves, the worry
usually resolves itself in one direction or the other. In the
embed-and-distroless shape, it resolves toward keeping one Dockerfile and
trusting what the builder throws away.
Build-time complexity is something the build host pays for, on hardware nobody attacks. Runtime simplicity is what gets scanned, attacked, and operated for the lifetime of the service. Spending the first to buy the second is one of the better trades a Dockerfile can make.
If a multi-stage Dockerfile in a project is making someone on the team nervous, and a second pair of eyes on what it actually ships would help, Get in touch. Happy readings!