NextJS hot reload with Docker is a powerful combination for seamless development environments

I've encountered issues trying to configure hot reload with Docker and NextJS. When I make changes and save a file, the server does not reload.

Below is the contents of the docker-compose.yml:

version: '3'
services:
  mainapp:
    build: ./mainapp
    restart: always
    volumes:
      - ./mainapp:/mainapp
  subapp:
    build: ./apps/subapp
    restart: always
    volumes:
      - ./apps/subapp:/subapp
  mainappdb:
    image: postgres:alpine
    restart: always
    environment:
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=...
    ports:
      - 5432:5432
    volumes:
      - mainappdb:/var/lib/postgres/mainappdb
  nginx:
    build: ./nginx
    ports:
      - 80:80
volumes:
  mainappdb:
    driver: local

This is the Dockerfile for mainapp:

FROM node:alpine

WORKDIR /usr/app

COPY ./ ./

EXPOSE 3000

CMD ["npm", "run", "dev"]

This is the Dockerfile for subapp:

FROM node:alpine

WORKDIR /usr/app

COPY ./ ./

EXPOSE 3000

CMD ["npm", "run", "dev"]

This is next.config.js for mainapp:

/** @type {import('next').NextConfig} */
const nextConfig = {
  webpackDevMiddleware: config => {
    config.watchOptions = {
      poll: 1000,
      aggregateTimeout: 300,
    }
    return config
  },
  experimental: {
    outputStandalone: true,
  },
  reactStrictMode: true,
  swcMinify: false,
  images: {
    domains: [...],
  },
}

module.exports = nextConfig

This is next.config.js for subapp:

/** @type {import('next').NextConfig} */
const nextConfig = {
  basePath: '/subapp',
  webpack: (config, context) => {
    config.watchOptions = {
      poll: 1000,
      aggregateTimeout: 300
    }
    return config
  },
  experimental: {
    outputStandalone: true,
  },
  reactStrictMode: true,
  swcMinify: true,
}

module.exports = nextConfig

The directory structure is as follows:

/myapplication/apps/subapp
    |
    |--> Dockerfile
    |
    |--> (all the nextjs code)
/myapplication/mainapp
    |
    |--> Dockerfile
    |
    |--> (all the nextjs code)
/myapplication/nginx
    |
    |--> Dockerfile
    |
    |--> default.conf
/myapplication/docker-compose.yml

Could the issue be related to my use of the WORKDIR instruction in the Dockerfile?

Answer №1

If you're facing issues, consider adding the following code snippet to your next.config.js file:

module.exports = {
  webpack: (config, _) => ({
    ...config,
    watchOptions: {
      ...config.watchOptions,
      poll: 800,
      aggregateTimeout: 300,
    },
  }),
}

This solution worked perfectly for me.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Assigning information to a button within a cell from a dynamically generated row in a table

I've been diligently searching through numerous code samples but have yet to find a solution to my current dilemma: My HTML table is dynamically generated based on mustache values and follows this structure: <tbody> {{#Resul ...

An ultimate guide to mapping a nested array in React.js

Problem: I am struggling to display the entire content of my array. The goal is to render all elements in the array, but so far I can only get one to show up. I have found that including [key] in the object fields is the only way to generate any output. ...

In production on IIS, Next.js displays a 404 error

After setting up a new Next.js application with a custom server and deploying it on IIS without making any modifications, I encountered an issue. While the application runs smoothly using 'npm run start' locally, it throws errors on IIS with mess ...

The hyperlink to a different webpage does not trigger any JavaScript functionalities or render any CSS styles

I am facing an issue with linking HTML pages that run Javascript and JQuery Mobile from another HTML page. My link setup is as follows: <a href="hours.html">Hours</a> The linking page and the linked pages are in the same directory. However, ...

Iterating through a for loop in Angular2 to send multiple GET requests to a Django backend

Currently, I'm facing a challenge with performing multiple GET requests using Angular2 within a Django/Python environment. After successfully making an API request and retrieving a list of users to determine the current user's ID, I utilize a .f ...

APNS functionality is supported by APN providers, but it is not compatible with NodeJS in a production

I've set up a nodeJS script to send APNs. In development, it always works flawlessly. However, when I switch to production, the notifications never go through. Oddly enough, when I use the same notification ID with my production certificate in Easy Ap ...

The navigation links in my React project are not appearing on the screen as expected

Hello everyone, I am relatively new to React and recently I have been attempting to utilize `react-router` in order to construct a Single Page Application. My goal is to link all the pages (such as Home, About, Login, etc) in the navigation bar using the & ...

Utilizing the "return" keyword in Javascript outside of function declarations

Exploring the Impact of Using the Return Keyword in JavaScript Scripts Beyond Functions in Browsers and Node.js Recently, I experimented with utilizing the return keyword in a Node.js script like so: #!/usr/bin/env node return 10; My initial assumption ...

Combining audio and video streams in Node.js

I'm currently developing a YouTube video downloader using the ytdl-core library. However, I've encountered an issue where it cannot fetch high quality videos with audio because they are stored in separate files on YouTube. My goal is to download ...

What is the proper method for utilizing a conditional header in an rtk query?

How can I implement conditional header authentication using rtk query? I need to pass headers for all requests except refresh token, where headers should be empty. Is it possible to achieve this by setting a condition or using two separate fetchBaseQuery ...

Tips for modifying string in key-value pairs on the client side (example using Paypal checkout demo)

Looking to integrate an online payment system into my small online business, I have decided on using PayPal. Their solution is user-friendly and can be found here: https://developer.paypal.com/demo/checkout/#/pattern/client However, I am facing an issue w ...

Keep a close eye on your Vue app to make sure it's

<template> <v-form :model='agency'> <v-layout row wrap> <v-flex xs12 sm12 lg12 > <v-layout row wrap> <v-flex xs12 md12 class="add-col-padding-right"> <v-radio-group v-mod ...

Ensuring a value is fully defined before passing it as a prop in a component

Is there a way to handle passing down state as a prop in a React component that is being fetched from an API using useEffect and axios? The state is initially set to "null", and I am encountering issues when trying to pass it down as a prop before it is ...

When node.js v6.11.2 is installed on Windows 7, it does not include the correct npm version

When trying to install node.js v6.11.2 on my Windows 7 computer, I am encountering an issue where it is installing the incorrect version of npm alongside it. Even after downloading the installer directly from node.js' website which claims that 6.11.2 ...

Is it possible to wait for two asynchronous actions using only one await statement?

I have a situation where I am dealing with a node module that exports a promise to resolve a database connection. Once this connection is resolved, I then need to use it to query records which involves another asynchronous operation. Is it possible to hand ...

Circular arrangement using D3 Circle Pack Layout in a horizontal orientation

I'm currently experimenting with creating a wordcloud using the D3 pack layout in a horizontal format. Instead of restricting the width, I am limiting the height for my layout. The pack layout automatically arranges the circles with the largest one ...

Mobile video created with Adobe Animate

Currently, I am designing an HTML5 banner in Adobe Animate that includes a video element. However, due to restrictions on iPhone and iPad devices, the video cannot autoplay. As a workaround, I would like to display a static image instead. Can anyone provid ...

Prevent Node.js Express from automatically creating sessions

When I activate the session option in express using app.use(express.session({secret: "12345"}));, a session cookie is automatically created when the user visits a page for the first time. Is there a way to turn off this automatic behavior and have more co ...

Tips for parsing JSON using JavaScript

I am looking to iterate through a JSON object that looks like {"key1":"val1","key2":"val2","key3":"val3"} in a loop. Here is my attempt: var inobj = '[{"key1":"val1","key2":"val2","key3":"val3"}]'; var obj = eval(inobj); for (var i = 0; i ...

Is there a way to include a button at the top of the Notiflix.Loading() overlay for closing or stopping it?

I've integrated the "notiflix" library into my project, and I'm currently using notiflix.loading.pulse() for a lengthy process. While this works perfectly fine, I would like to add a button (for closing/stopping the process) on top of the loading ...