The short answer
Our Dockerfile copied the Next.js server and its static chunks in two separate COPY steps. With a build cache, each step could take files from a different run of the build, so the image held a server from one build and chunks from another. We now copy everything from one step as one layer, and the image build fails if the chunks don't match the server's BUILD_ID.
On this page
In November 2026 the browser tests for Forkly, our restaurant ordering platform, went red on code that hadn’t changed. Two smoke tests timed out. Pages loaded their HTML, then asked for JavaScript chunks that answered 404, so they never became interactive. Nothing in the application code had changed since the last green run.
The cause was in the Dockerfile. This post shows what went wrong, how we proved it, and the two-line check that now stops it at build time. We added the same check to every Next.js app we run.
What did the Dockerfile do?
A common Dockerfile for a Next.js app in standalone mode builds in one stage and copies three things into a small runtime image:
COPY --from=build /app/apps/web/.next/standalone ./
COPY --from=build /app/apps/web/.next/static apps/web/.next/static
COPY --from=build /app/apps/web/public apps/web/public
This looks safe: all three lines copy from the same build stage. We used it for months.
Why can two COPY lines come from two different builds?
Two facts combine.
- A Next.js build isn’t byte-identical between runs. Each build gets a new
BUILD_ID, and the chunk file names change with it. The server only knows the chunks of its own build. - Build caches work per step. Our CI builds with a shared Docker build cache. When the cache holds results from several runs of the build stage, the copy steps aren’t guaranteed to resolve to the same one.
In the failing image the server came from a build of the previous day. The static folder held chunks from other builds. The server rendered pages that pointed at chunk names it expected, and those files weren’t there.
How did we prove it?
We didn’t want to fix it on a guess. We built the image the way CI builds it, with the same Docker daemon, the same cache and the same build arguments. The result was the same mixed image: a server with one BUILD_ID and chunks belonging to other builds.
In a working Next.js image, .next/static/<BUILD_ID>/ exists for the server’s own BUILD_ID. In the broken image that folder was missing. That gave us both the diagnosis and the test.
What did we change?
Two things.
One build step produces one output folder. The build stage now builds and then assembles the server, its chunks and public/ into a single folder in the same RUN step. The runtime stage copies that folder once:
RUN pnpm --filter web build && \
mkdir -p /out && cp -a apps/web/.next/standalone/. /out/ && \
mkdir -p /out/apps/web/.next/static /out/apps/web/public && \
cp -a apps/web/.next/static/. /out/apps/web/.next/static/ && \
cp -a apps/web/public/. /out/apps/web/public/
# runtime stage
COPY --from=build /out ./
The files are now one layer from one run. They can’t come from two builds.
The image build checks that they belong together:
RUN test -d "apps/web/.next/static/$(cat apps/web/.next/BUILD_ID)" \
|| { echo "static chunks are not from build $(cat apps/web/.next/BUILD_ID)" >&2; exit 1; }
If the chunks for the server’s own build are missing, the image build fails. A broken image never reaches a test run or a server.
We checked the guard both ways: it fails on the mixed image we reproduced, and it passes on the image that was running in production.
What did this teach us?
- The browser tests caught it, but late. The smoke tests did their job, but they took minutes to time out and pointed at the symptom (a page that never hydrates), not the cause. A one-line check at build time fails in seconds and names the cause.
- “Same stage” isn’t “same run”. If several files must come from the same build, produce them in one step and copy them once.
- Fix the class of bug, not the instance. We run more than one Next.js app with the same Dockerfile pattern. The single-layer copy and the guard went into all of them, and into the template we start new apps from.
Do you have the same pattern?
If your Dockerfile has two or more COPY --from lines that copy outputs of the same non-reproducible build, you have the same risk. It shows up most when CI uses a shared or persistent build cache. The fix is the one above: one output folder, one COPY, one check that the parts match.