Docker-based NextJS project not auto-refreshing with npm run dev

I’m having trouble with my NextJS project in Docker. I’ve set it up and it runs, but there’s a problem with auto-reloading.

When I use npm run dev (which is set to next dev in my package.json), the page loads fine. But when I make changes to page.tsx, it doesn’t update automatically. I have to manually refresh to see the changes.

I’ve checked that the files are mapped as a volume, but that doesn’t seem to be the issue. The container is working because I can see my changes after refreshing.

In the console, I’m seeing two errors:

WebSocket connection to 'ws://mysite.local/_next/webpack-hmr' failed:

Error: A listener indicated an asynchronous response by returning true, but the message channel closed before a response was received

These errors don’t show up in the Docker logs though. Any ideas on how to fix this auto-reload problem?

I’ve dealt with this issue before in Docker environments. One often overlooked solution is to adjust your Next.js configuration. Try adding the following to your next.config.js file:

webpack: (config, { isServer }) => {
if (!isServer) {
config.watchOptions = {
poll: 1000,
aggregateTimeout: 300,
}
}
return config
}

This configuration forces webpack to use polling, which can be more reliable in containerized setups. Additionally, ensure your Docker container has the correct permissions to access and modify files in the mounted volume. Sometimes, file ownership issues can prevent proper file watching.

If these don’t work, you might want to investigate your host machine’s file system. Some file systems don’t propagate change events properly to Docker, especially on Windows or when using certain network drives.

hey, i had the same problem. try adding this to ur docker-compose.yml:

environment:

  • CHOKIDAR_USEPOLLING=true

this forces nextjs to use polling for file changes. worked for me. also, check ur volumes are set up right. good luck!

I’ve encountered a similar issue with Docker and NextJS before. It’s frustrating when hot reloading doesn’t work as expected. In my experience, this often boils down to file watching issues within the Docker environment.

One solution that worked for me was adding the WATCHPACK_POLLING environment variable to my Docker setup. You can do this by adding the following to your Dockerfile:

ENV WATCHPACK_POLLING=true

This forces NextJS to use polling instead of file system events, which can be more reliable in containerized environments.

Another thing to check is your Docker Compose file (if you’re using one). Make sure you’re using the correct volume mapping for your project directory. It should look something like:

volumes:

  • .:/app
  • /app/node_modules

This ensures your local changes are reflected in the container while preserving the node_modules inside Docker.

If these don’t solve it, you might want to look into your network setup. Sometimes, WebSocket issues can be caused by proxy configurations or network restrictions. Double-check your Docker network settings and any reverse proxies you might be using.

Hope this helps! Let me know if you need any clarification on these steps.