Showing updates in browser with hitting refresh using Docker to deploy for Vue.js

Viewed 626

I am new to docker and following the tutorial on https://cli.vuejs.org/guide/deployment.html#docker-nginx.

I have managed to get it work, but wondering if there is a way to get the browser to update without having to hit the refresh button. Here are the steps I take.

  1. Change content.
  2. Stop container.
  3. Remove container.
  4. Build container.
  5. Run container.
  6. Refresh browser.

I am wondering if there is a way to avoid step 6 to see the new content in the browser.

Here is my docker file

FROM node:latest as build-stage
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY ./ .
RUN npm run build

FROM nginx as production-stage
RUN mkdir /app
COPY --from=build-stage /app/dist /app
COPY nginx.conf /etc/nginx/nginx.conf

and here is my nginx.conf file

user  nginx;
worker_processes  1;
error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;
events {
  worker_connections  1024;
}
http {
  include       /etc/nginx/mime.types;
  default_type  application/octet-stream;
  log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                    '$status $body_bytes_sent "$http_referer" '
                    '"$http_user_agent" "$http_x_forwarded_for"';
  access_log  /var/log/nginx/access.log  main;
  sendfile        on;
  keepalive_timeout  65;
  server {
    listen       80;
    server_name  localhost;
    location / {
      root   /app;
      index  index.html;
      try_files $uri $uri/ /index.html;
    }
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
      root   /usr/share/nginx/html;
    }
  }
}
1 Answers

Problem

Hot reloading is generally implemented using file system specific event APIs to determine when and what to “reload”. Since you’re using Vue in a docker container running a different OS, Vue has no way of communicating with your file system via API (and shouldn’t, for obvious security reasons).

Solution

Ensure Hot Reloading Is Enabled

You’ll need to ensure hot reloading is enabled in Vue.us and force Vue.is to use polling rather than sockets to determine when to reload.

Ensure hot reloading is enabled by disabling minification in development mode, webpack target is not “node” and NODE_ENV is not “production”.

Use Polling

Update webpack config to include the following:

watchOptions: {
    poll: 1000 // Check for changes every second
  }

or

Set environment variable CHOKIDAR_USEPOLLING in your development docker or docker compose file to true.

References

Disabling Hot Reload in Vue.js: https://vue-loader.vuejs.org/guide/hot-reload.html#usage

Watch Options: https://webpack.js.org/configuration/watch/#watchoptionspoll

Related