The reason why Hot module replacement [HMR] is not working in docker is because of the way Webpack looks for file changes in a directory, it uses fsevent
and inotify
. These are modules webpack uses to watch the files in a specified directory. For using webpack-dev-server in a docker image, its best explained by Mihail Ignatiev and HosseinAgha.
Also, you can change the port number the webpack-dev-server
command uses by specifying it in the webpack.config.js.
var path = require('path');
module.exports = {
// It can be changed using port key
devServer: {
contentBase: path.join(__dirname, 'dist'),
compress: true,
port: 9000
}
};
To answer your question, webpack-dev-server
will include a script in your bundle that connects to a websocket to reload when a change occurs in any of your files. The --public
flag makes sure the script knows where to look for the websocket. The server will use port 8080 by default, so we can also specify a custom port using --port
cli option.
Also, inline mode is recommended for hot reload as it includes an HMR trigger from the websocket. Polling mode can be used as an alternative, but requires an additional entry point, webpack/hot/poll?1000
.
You can use it as follows,
webpack-dev-server --inline
For an in depth insight into how webpack-dev-server
uses websockets, you can refer to the official documentation.