Docker bind mount volumes do not propagate changes events watched by angular `ng serve` execution

社会主义新天地 提交于 2019-12-13 12:09:46

问题


Following these steps:

  1. Define the Dockerfile:

    FROM node:alpine
    
    RUN yarn global add @angular/cli
    RUN yarn global add node-sass
    
    RUN mkdir /volumes
    
    WORKDIR /volumes
    
    EXPOSE 4200
    
    ENTRYPOINT ["ng"]
    
  2. Build an image from this Dockerfile:

    docker build -t my_angular_image .
    
  3. Use the image to create a new angular app :

    // Create the new app
    docker run --rm --mount type=bind,src=$PWD,dst=/volumes my_angular_image new my-app --directory app --style scss
    // Change ownership of the generated app
    sudo chown -R $USER:$USER .
    
  4. Based on the image, run a container bind mounting the app volume:

    docker run -p 4200:4200 --mount type=bind,src=$PWD/app,dst=/volumes my_angular_image serve --host 0.0.0.0
    

Results: The first compilation works as expected and the container serves the app. However, when changing the value (from host) of a file which has to be watched by ng serve in container, a new angular build is not triggered (and so, the served app is not updated).

Question: Does someone know why changing the value of a bind mount volume on host does not trigger the angular ng serve update in container (as it does when not using Docker)?

Environment:

  • OS : Ubuntu 16.04
  • Docker : 18.01.0-ce

回答1:


there is a hidden option for angular-cli.json named poll by specifying which you can change the way how underlining webpack watches for file updates. Links:

  1. https://webpack.js.org/configuration/watch/#watchoptions-poll
  2. https://github.com/angular/angular-cli/pull/1814
  3. https://github.com/angular/angular-cli/wiki/angular-cli#angular-cli-config-schema



回答2:


In order to make the example working, replace step 3 by the commands below:

// Create the new app
docker run --rm --mount type=bind,src=$PWD,dst=/volumes my_angular_image new my-app --directory app --style scss
// Change ownership of the generated app
sudo chown -R $USER:$USER .
// Configure angular-cli polling:
sed -i 's/\"styleExt\": \"scss\",/"styleExt": "scss", "poll": 1000,/g' $PWD/app/.angular-cli.json

Credits:

  • @PavelAgarkov's answer and its usefull links.


来源:https://stackoverflow.com/questions/48351225/docker-bind-mount-volumes-do-not-propagate-changes-events-watched-by-angular-ng

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