【问题标题】:Docker bind mount volumes do not propagate changes events watched by angular `ng serve` executionDocker 绑定挂载卷不会传播由 Angular `ng serve` 执行观察到的更改事件
【发布时间】:2018-06-29 07:00:55
【问题描述】:

按照以下步骤操作:

  1. 定义 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. 从此 Dockerfile 构建映像:

    docker build -t my_angular_image .
    
  3. 使用图像创建一个新的 Angular 应用程序:

    // 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. 基于镜像,运行容器绑定挂载应用卷:

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

结果: 第一次编译按预期工作,容器为应用程序提供服务。但是,当更改必须由容器中 ng serve 监视的文件的值(来自主机)时,不会触发新的角度构建(因此,服务的应用程序不会更新)。

问题: 有人知道为什么更改主机上绑定挂载卷的值不会触发容器中的 ng serve 角度更新(就像不使用 Docker 时那样)?

环境:

  • 操作系统:Ubuntu 16.04
  • Docker : 18.01.0-ce

【问题讨论】:

  • 你在哪个操作系统上运行 docker?
  • 是的,这不适用于带有 Linux 容器的 Windows docker 主机。
  • @TarunLalwani 我添加了一个环境部分,我在 Ubuntu Xenial 上。
  • @Brian 即使我不在 Windows docker 主机上,我也很好奇,你能解释一下为什么这在带有 Linux 容器的 Windows docker 主机上不起作用吗?

标签: angular docker angular-cli dockerfile mounted-volumes


【解决方案1】:

为了使示例正常运行,请将步骤 3 替换为以下命令:

// 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

致谢:

  • @PavelAgarkov 的回答及其有用的链接。

【讨论】:

    【解决方案2】:

    angular-cli.json 有一个隐藏选项,名为 poll,通过指定可以更改下划线 webpack 监视文件更新的方式。 链接:

    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

    【讨论】:

      猜你喜欢
      • 2017-07-05
      • 2021-05-08
      • 2020-07-26
      • 1970-01-01
      • 1970-01-01
      • 2023-03-23
      • 2018-12-18
      • 1970-01-01
      • 2021-10-26
      相关资源
      最近更新 更多