Next.js Hot Reload Not Working in Docker? I Tested 8 Setups
You run a Next.js app in Docker, edit a file, save, and the browser shows the old page. No error, no...
You run a Next.js app in Docker, edit a file, save, and the browser shows the old page. No error, no "compiling" line in the logs. The dev server simply never notices the change. That is what Next.js hot reload not working in Docker looks like almost every time.
I tested this on Windows 11 with Docker Desktop and a fresh Next.js 16.3.3 app: eight container setups, three edits each, timed by a script. Three of the usual fixes did nothing. Two worked with webpack, and one worked with Turbopack, at the same speed as running Next.js without Docker. Here are the results and the files.
Why hot reload stops working in Docker
The usual dev setup mounts your project folder into the container (.:/app), so the container sees your edits. On Windows and macOS, Docker runs Linux inside a small virtual machine, and the mounted folder is served into that VM by Docker Desktop's file sharing. It is not a normal Linux disk. The file contents arrive, but the change notifications that Linux file watchers wait for (inotify events) do not reliably come with them. The dev server is waiting for an event that never fires.
In my tests it went further than "edits from Windows are missed". With Turbopack, I also rewrote the page from inside the container, on the mounted folder. It was not picked up either, in two tries. So it is not about which side makes the edit. It is about where the file lives.
That leaves two ways around it. Either the watcher stops waiting for events and checks the files on a timer (polling), or the files are copied onto the container's own filesystem, where the events work. The results below show which one works with which bundler.
First, check that this is really the problem
Before changing anything, make sure the edit actually reaches the container and only the reload is missing. Save a visible change, then print the file from inside the container:
docker compose exec web cat src/app/page.tsx
If the new text is there but the browser still shows the old page after a hard refresh, and docker compose logs web shows no compile line after your save, you have the watcher problem this post is about. In my Turbopack test that was exactly the picture: the container had the new file, the page served the old one, and the log ended at the last GET / 200.
If the file in the container is old, the problem is earlier. You are editing a different folder from the one that is mounted, or the service has no volumes: entry at all and runs the copy that was baked into the image. Rebuilding with docker compose up --build will show the change once, but it will not reload. Fix the mount, or use compose watch as described below.
One more check: touch is not a good test. Running touch src/app/page.tsx inside the container did not trigger a reload, but that tells you little, because the content did not change. Test with a real edit.
The test: 8 setups, timed
A script started each container, waited until the page served a marker text, then rewrote src/app/page.tsx on Windows, the way an editor does, and requested the page every half second for up to 30 seconds. Three edits per setup. As a control, Turbopack running natively on Windows picked up the same edits in 0.1 to 0.7 seconds, so the method itself works.
| Setup | Edit picked up? | Idle CPU / RAM |
|---|---|---|
| Turbopack, bind mount | No (0 of 3) | 0.00% / 224 MiB |
| Turbopack, bind mount, watchOptions.pollIntervalMs | No (0 of 3) | 3.30% / 260 MiB |
| Turbopack, bind mount, WATCHPACK_POLLING + CHOKIDAR_USEPOLLING | No (0 of 3) | 0.00% / 215 MiB |
| webpack, bind mount | No (0 of 3) | 0.01% / 585 MiB |
| webpack, bind mount, WATCHPACK_POLLING=true | Yes: 1.8, 0.8, 1.0 s | 0.00% / 574 MiB |
| webpack, bind mount, watchOptions.pollIntervalMs | Yes: 0.9, 0.7, 1.1 s | 1.00% / 687 MiB |
| Turbopack, docker compose watch (sync src) | Yes: 0.6, 0.6, 0.6 s | 0.00% / 222 MiB |
| Turbopack, docker compose watch (sync whole project) | Yes: 0.7, 0.6, 0.6 s | 0.00% / 237 MiB |
Setup: Docker Desktop 4.99.0, Compose v5.7.0, image node:22-alpine, Next.js 16.3.3 with React 19.2.8. Idle figures come from docker stats 8 seconds after the last edit. One run per setup, so treat the CPU numbers as rough.
What does not work: polling with Turbopack
Next.js 16 uses Turbopack for next dev by default, and that is where most people get stuck. The fixes that circulate for this problem all come from the webpack era:
WATCHPACK_POLLING=trueis read by Watchpack, webpack's file watcher. Turbopack does not use Watchpack, so the variable does nothing.CHOKIDAR_USEPOLLING=trueis for chokidar, which Next.js does not use for this. Also nothing.watchOptions.pollIntervalMsinnext.config.tsis the interesting one. Next.js 16.3.3 does pass it to Turbopack: it is innext/dist/server/dev/hot-reloader-turbopack.js, in thewatchoptions given to the project. And the container's idle CPU went from 0% to 3.3%, so something was polling. It still missed all three edits.
I don't know why Turbopack's polling misses the change, and I won't guess. What I can say is that on this setup it did not work in any of three tries, while the same option worked with webpack.
Fix 1: docker compose watch (works with Turbopack)
Compose has a feature built for exactly this. Instead of mounting your folder, docker compose watch watches the files on your machine and copies each changed file into the container. The copy lands on the container's own Linux filesystem, so Linux sends the normal change event and Turbopack reacts immediately. In my test that took 0.6 seconds, close to native speed, with no polling and 0% idle CPU.
Dockerfile.dev. The source has to be copied into the image, because nothing is mounted:
FROM node:22-alpine
WORKDIR /app
COPY package.json package-lock.json ./
RUN npm ci
COPY . .
EXPOSE 3000
CMD ["npx", "next", "dev", "-H", "0.0.0.0"]
.dockerignore, so your Windows node_modules never reaches the Linux image:
node_modules
.next
compose.yaml. Note that there is no volumes: section:
services:
web:
build:
context: .
dockerfile: Dockerfile.dev
ports:
- "3000:3000"
develop:
watch:
- action: sync
path: .
target: /app
ignore:
- node_modules/
- .next/
- action: rebuild
path: package.json
Start it with docker compose watch instead of docker compose up. It builds the image, starts the container and keeps syncing until you stop it. The -H 0.0.0.0 matters: without it the dev server listens only inside the container and the browser on your machine can't reach it.
The rebuild rule rebuilds the image when package.json changes, so a new dependency gets installed in the container. That rule is from the Compose documentation. I tested only edits to page files, not a dependency change.
Two limits to know. Sync goes one way, from your machine into the container: files the container creates, such as a generated Prisma client, do not appear on your disk. And the ignore entries matter. If node_modules/ is synced, your Windows packages overwrite the Linux ones in the container and native packages break.
Fix 2: webpack with polling (works with a bind mount)
If you want to keep the classic bind mount, or your team already has a compose file built around one, run the dev server with webpack and turn on polling:
services:
web:
build:
context: .
dockerfile: Dockerfile.dev
ports:
- "3000:3000"
environment:
- WATCHPACK_POLLING=true
command: ["npx", "next", "dev", "--webpack", "-H", "0.0.0.0"]
volumes:
- .:/app
- /app/node_modules
- /app/.next
Edits showed up in about a second. Instead of the environment variable you can set watchOptions: { pollIntervalMs: 1000 } in next.config.ts, which gave the same speed. The two anonymous volumes keep the container's own node_modules and .next from being replaced by the folders on your disk.
The costs: webpack is the slower bundler, the container used about 2.5 times the memory of the Turbopack one (574 to 687 MiB against 215 to 260 MiB), and polling keeps checking files even when you are not editing. With pollIntervalMs the container sat at 1% CPU when idle. On a big project with thousands of files, expect more.
Other options I did not test
- Keep the project inside WSL2. On Windows, cloning the repo into the WSL2 Linux filesystem (not
/mnt/c) and running Docker from there is often recommended, because the files never cross from Windows into Linux. On this PC, Docker Desktop's WSL integration is off for the Ubuntu distro, and the mount failed withaccessing specified distro mount service, so I have no result for it. - VS Code Dev Containers with the source in a container volume. Same idea as compose watch: the files live on Linux. Not tested.
- A Linux host. With Docker on Linux there is no VM in between, and bind mounts deliver change events. I did not test that here.
Which fix to use
- New setup, or you can change the compose file: if Next.js hot reload is not working in Docker with Turbopack, switch to
docker compose watch. You keep Turbopack, it was the fastest container setup, and nothing polls. - Existing setup built on a bind mount: add
--webpackandWATCHPACK_POLLING=true. Two lines, and it works. - You only need Docker because of a Node version mismatch: you may not need it at all. A version manager and an
.nvmrcfile solve that, and hot reload works natively. I compared the options in running Next.js on a different Node version.
Two smaller traps from the same session. Port 3000 was already taken on my machine, so the container is mapped to 3300 in my tests. If yours won't start, see EADDRINUSE on port 3000. If Next.js says another dev server is already running, that is a different problem with its own fix. And if images, volumes and compose files are new to you, start with the Docker tutorial.
Originally published on KnowledgeMarkG.
Sumber artikel open-source oleh Kausar Raza di DEV Community — Baca artikel asli. Topik:
#nextjs#docker#webdev#turbopack

Komentar