Enabling webpack hot-reload in a docker application

落爺英雄遲暮 提交于 2019-12-04 09:23:59

I couldn't make webpack or webpack-dev-server watch (--watch) mode work even after mounting my project folder into container.
To fix this you need to understand how webpack detects file changes within a directory.
It uses one of 2 softwares that add OS level support for watching for file changes called inotify and fsevent. Standard Docker images usually don't have these (specially inotify for linux) preinstalled so you have to install it in your Dockerfile.
Look for inotify-tools package in your distro's package manager and install it. fortunately all alpine, debian, centos have this.

Docker & webpack-dev-server can be fully operational without any middleware or plugins, proper configuration is the deal:

devServer: {
  port: 80, // use any port suitable for your configuration
  host: '0.0.0.0', // to accept connections from outside container
  watchOptions: {
      aggregateTimeout: 500, // delay before reloading
      poll: 1000 // enable polling since fsevents are not supported in docker
  }
}

Use this config only if your docker container does not support fsevents.

For performance efficient way check out HosseinAgha answer #42445288: Enabling webpack hot-reload in a docker application

try doing this:

  1. Add watchOptions.poll = true in webpack config.

    watchOptions: { poll: true },

  2. Configure host in devServer config

    host:"0.0.0.0",

In my case, I had a VueJS application. I followed the tutorial in the post Enabling Hot-Reloading with vuejs and vue-cli in Docker. My solution was to modify my dockerfile with this command:

 FROM node:lts-alpine
 ...
 RUN yarn global add @vue/cli
 ...

It allowed container to detected the changes in my code and reloaded in my browser. In my case, it wasn't necessary create a vue.config.js with the devServer.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!