The starting point
Our client sites run on servers we manage, deployed with Dokploy. Moving onto our own infrastructure gave us control, but it didn't make deploys fast. A push to one of our larger sites took 11 minutes 51 seconds to go live.
I assumed most of that was Next.js compiling. It wasn't. Reading the build log step by step, building the app itself took under three minutes. The rest was the build tooling around it.
Where the time went
Dokploy was building with Railpack, which works out how to build an app without a Dockerfile. That convenience cost time on every deploy:
- A new build container each time, so nothing was cached and every deploy downloaded all 1,689 packages again
- System packages installed from scratch on every run
- The whole dependency tree shipped in the image, not just what the server needs
- About two and a half minutes exporting that image from the build container to Docker
- A full TypeScript check, 68 seconds, that CI had already run on the pull request
What I changed
Each site now builds from its own Dockerfile on the server's normal Docker, so the build cache survives between deploys.
- Dependencies install in their own layer, which is reused until the lockfile changes. A cache mount keeps pnpm's store, so a dependency change only downloads what's new
- Next's build cache is kept between deploys, which halved the compile time
- The image ships Next's standalone server instead of the full
node_modules - The type check runs in CI only. The deploy skips it
- A
.dockerignoreleaves out about 190 MB of Payload migration snapshots that the build never reads
FROM base AS depsCOPY package.json pnpm-lock.yaml .npmrc ./RUN --mount=type=cache,id=pnpm-store,target=/pnpm/store \ pnpm install --frozen-lockfile --store-dir /pnpm/store
FROM base AS builderCOPY --from=deps /app/node_modules ./node_modulesCOPY . .# CI already type-checks every pull requestENV SKIP_BUILD_TYPECHECK=1RUN --mount=type=cache,id=next-cache,target=/app/.next/cache \ pnpm run buildCache mounts only help if the cache survives. The first builds on the new setup still took about four and a half minutes, because the build cache was being cleared between deploys. Once it was kept, a typical deploy dropped to 80 seconds.
What stayed the same
Migrations still run on every deploy, so a schema change still applies itself. Pages are still pre-rendered at build time, so visitors get the same speed as before. I wasn't willing to trade either for a faster build.
Two things that caught me out
The server crashed on boot
Node 22.12 lets require() load a package's ES module build, so it went looking for @swc/helpers' esm folder. Next's standalone output only copies the CommonJS files, so the server stopped with MODULE_NOT_FOUND and the proxy returned a 502. Starting Node with --no-experimental-require-module makes it use the files the build actually copied.
It has to listen on every address
Docker Swarm sets HOSTNAME to the container's ID, and Next's standalone server binds to HOSTNAME. The start command sets HOSTNAME=0.0.0.0 so it listens on every interface. Setting it in the image isn't enough, because Swarm overrides it.
The result
- A typical deploy, a code change with the same dependencies: 11m 51s down to 1m 20s
- A redeploy with nothing changed: 18 seconds
- A deploy with an empty cache: about four and a half minutes, still well under half the old time
Measured on the first client site to move over. The rest follow the same pattern.
What I'd do differently
Read the build log before changing anything. I went in expecting to tune Next.js, and most of the time had nothing to do with it.