Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular CLI & Docker: ng serve change detection very slow

Tags:

docker

angular

I have an Angular 5.2 app running in a Docker container, and for development purposes I am using Angular CLI's ng-serve to serve the app which provides auto-reloading on changes. My original working serve command;

ng serve --host 0.0.0.0 --port 80 --disable-host-check

This functions fine, and I can access the served app from the host machine as expected.

To the above, thanks to various sources, I added the --poll 1000 flag, which tells the CLI to poll files for changes every 1000ms, without which it never detects changes, and thus never recompiles and updates. My issue here is that between a file being changed and the change being detected within the docker container triggering the rebuild, there is a delay of up to 15-20 seconds. I have confirmed that the file changes are translated into the container as I can cat the edited file immediately on save within the container and see the changes. I can change the poll timeout to 1ms and this does reduce the delay to a few seconds, but this obviously isn't ideal and the delay is still substantial.

Why would such a delay exist?


  • OSX version: 10.11.6
  • Docker version: Docker version 18.04.0-ce, build 3d479c0
  • Docker Machine: 0.14.0, build 89b8332
  • Docker Compose: 1.21.0

// docker-compose.yml
web-client:
  build:
    context: ../web-client/
    dockerfile: ./docker/Dockerfile
  ports:
    - "80:80"
  volumes:
    - ../web-client/:/var/www/web-client
  container_name: web-client



// Dockerfile
FROM node:9.11

COPY ./ /var/www/web-client

ENTRYPOINT ["/var/www/web-client/docker/entrypoint.sh"]



// entrypoint.sh
#!/usr/bin/env bash

cd /var/www/web-client

yarn run start



// package.json
"scripts": {
    ...
    "start": "ng serve --host 0.0.0.0 --port 80 --poll 1 --disable-host-check",
    ...
}
like image 384
James Crinkley Avatar asked Apr 19 '18 21:04

James Crinkley


People also ask

What is an Angular CLI?

The Angular CLI is a command-line interface tool that you use to initialize, develop, scaffold, and maintain Angular applications directly from a command shell.

Is Angular and Angular CLI same?

In a very simple words, Angular CLI is a command line interface for writing or easily setting/building up an angular application. Angular js - is a older version of Angular (version 1. x) which is a open-source JavaScript-based front-end web application framework.

Is Angular CLI required?

Angular CLI stands for Angular Command Line Interface. As the name implies, it is a command line tool for creating angular apps. It is recommended to use angular cli for creating angular apps as you don't need to spend time installing and configuring all the required dependencies and wiring everything together.

What is the latest Angular CLI?

The Angular latest Official stable version is Angular v13. 2.5, which is released on 2nd March 2022.


1 Answers

Here is my setup that is fairly fast,

docker compose, exposes port 4200 to the outside, mounts src folder to detect changes:

version: '2'
services:
  web:
    container_name: my-spa-dev
    build:
      context: ./
      dockerfile: Dockerfile.test
    ports:
      - "4200:4200"
    volumes:
      - ./src:/usr/src/app/src

Dockerfile.test:

FROM node:10
WORKDIR /usr/src/app

COPY package.json ./
COPY yarn.lock ./

RUN yarn install

COPY . .

CMD ["yarn", "start"]

run with docker-compose -f docker-compose.dev.yml up

like image 144
golfadas Avatar answered Oct 11 '22 01:10

golfadas