Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hot reload not working with webpack-dev-server and docker

Using Ubuntu Linux with docker installed. No VM.

I have build a docker image with a vuejs application. To enable hot reload I start the docker container with:

docker run -it -p 8081:8080 -e "HOST=0.0.0.0" -v ${PWD}:/app/ -v /app/node_modules --name my-frontend my-frontend-image

It starts up fine and I can access it from my host browser on localhost:8081. But when I make changes to the source files and save those changes they are not reflected in my browser before I press F5 (hot reload does not work).

Some details below:

package.json

  "scripts": {
    "dev": "webpack-dev-server --inline --progress --config build/webpack.dev.conf.js",
    "start": "npm run dev",

build/webpack.dev.conf.js

  devServer: {
    clientLogLevel: 'warning',
    ...
    hot: true,
    ...
    watchOptions: {
      //poll: config.dev.poll,
      //aggregateTimeout: 500, // delay before reloading
      poll: 100 // enable polling since fsevents are not supported in docker

    }

Tried to modify the watchOptions but it has no effect.

EDIT:

Based on below answer I have tried to pass: CHOKIDAR_USEPOLLING=true as an environment variable to docker run:

docker run -it -p 8081:8080 -e "HOST=0.0.0.0" -e "CHOKIDAR_USEPOLLING=true" -v ${PWD}:/app/ -v /app/node_modules --name my-frontend my-frontend-image

But it has not effect - still not able to hot reload my changes. Also in the provided link it says:

Update/Clarification: This problem only occurs when running your Docker engine inside a VM. If you are on Linux for both Docker and for coding you do not have this problem.

So don't think the answer applies to my setup - I am running Ubuntu Linux on my machine where I have installed docker. So no VM setup.

Another update - based on the comment below on changing the port mapping:

  # Hot reload works!
  docker run -it -p 8080:8080 -e "HOST=0.0.0.0" -v ${PWD}:/app/ -v /app/node_modules --name my-frontend my-frontend-image

  # Hot reload fails!  
  #docker run -it -p 8081:8080 -e "HOST=0.0.0.0" -v ${PWD}:/app/ -v /app/node_modules --name my-frontend my-frontend-image

So if I port map to 8080:8080 instead of 8081:8080 hot reload works! Notice the application comes up in both cases when I access it on my host browser on localhost on the before mentioned ports. Its just that hot reload only works when I map the application to 8080 on my host.

But why??

Now if I do:

PORT='8081'
docker run -it -p "${PORT}:${PORT}" -e "HOST=0.0.0.0" -e "PORT=${PORT}" -v ${PWD}:/app/ -v /app/node_modules --name my-frontend my-frontend-image

Hot reload of course works. But still not sure why I cannot map internal container port 8080 to 8081 externally on the host.

Btw; I don't see the problem at all if I use vue-cli-service serve instead - everything works out of the box.

like image 991
u123 Avatar asked Nov 14 '19 16:11

u123


2 Answers

I am not a VueJS user at all, never worked with it, but I use Docker heavily for my development workflow, and in the past I experienced a similar issue.

In my case the Javascript that was sent to the browser was trying to connect with the internal port of the docker container 8080, but once the mapped one for the host was 8081, the JS in the browser was not able to reach 8080 inside the docker container, therefore hot reload was not working.

So it seems to me that you have the same scenario as me, thus you need to configure in your VueJS app the hot reload to listen in the same port you want to use in the host, or just use the same port for both as you already concluded that it works.

like image 150
Exadra37 Avatar answered Nov 18 '22 07:11

Exadra37


If watchOptions doesnt work, you can try out the other option:

 environment:

  - CHOKIDAR_USEPOLLING=true

As per docs here:

“If watching does not work for you, try out this option. Watching does not work with NFS and machines in VirtualBox.”

Reference:

https://daten-und-bass.io/blog/enabling-hot-reloading-with-vuejs-and-vue-cli-in-docker/

like image 1
Gauravsa Avatar answered Nov 18 '22 08:11

Gauravsa