首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Docker中加速Angular的开发服务器启动?

如何在Docker中加速Angular的开发服务器启动?
EN

Stack Overflow用户
提问于 2020-09-29 21:56:18
回答 1查看 681关注 0票数 1

我在通过Docker容器运行一个Angular应用程序时遇到了一个问题,而源代码是通过卷附加的。ng serve的启动速度非常慢。

我的目标是(在Windows上)实现开发过程的对接。作为参考,可以在GitHub:https://github.com/csgulyas/node-docker-angular上找到这个虚拟项目

我有以下版本的东西:

代码语言:javascript
复制
Docker version 19.03.12, build 48a66213fe
docker-compose version 1.27.2, build 18f557f9

Running under WSL2:
  NAME                   STATE           VERSION
* Ubuntu-20.04           Running         2
  docker-desktop-data    Running         2
  docker-desktop         Running         2

除了通过ng new生成一个angular项目之外,我没有做任何其他事情,并且添加了

the Dockerfile

代码语言:javascript
复制
FROM node:12.18.4-alpine AS dev # latest LTS node

EXPOSE 4242 # just so it isn't 4200
EXPOSE 49153 # apparently this is for change detection

USER node

RUN mkdir -p /home/node/.npm-global
ENV PATH=/home/node/.npm-global/bin:$PATH
ENV NPM_CONFIG_PREFIX=/home/node/.npm-global

RUN npm install -g @angular/cli
WORKDIR /app

CMD ["ng", "serve", "--port", "4242", "--host", "0.0.0.0", "--disable-host-check", "--poll", "1000"]

docker-compose.yml

代码语言:javascript
复制
version: '3.4'

services:
    angularapp:
        container_name: angularapp
        image: ${DOCKER_REGISTRY-}webapp
        build:
            context: ./demo-frontend
            dockerfile: Dockerfile
        ports:
            - "4200:4242"
            - "49153:49153"
        environment:
            NODE_OPTIONS: --max_old_space_size=4096
        volumes:
            - ./demo-frontend:/app

在监视容器的输出时,我得到了以下信息

代码语言:javascript
复制
angularapp    | 2020-09-29T13:18:32.802573300Z Your global Angular CLI version (10.1.3) is greater than your local
angularapp    | 2020-09-29T13:18:32.802597400Z version (9.1.12). The local Angular CLI version is used.
angularapp    | 2020-09-29T13:18:32.802601300Z
angularapp    | 2020-09-29T13:18:32.802603800Z To disable this warning use "ng config -g cli.warnings.versionMismatch false".
angularapp    | 2020-09-29T13:19:50.000172200Z WARNING: This is a simple server for use in testing or debugging Angular applications
angularapp    | 2020-09-29T13:19:50.000214200Z locally. It hasn't been reviewed for security issues.
angularapp    | 2020-09-29T13:19:50.000221200Z
angularapp    | 2020-09-29T13:19:50.000223800Z Binding this server to an open connection can result in compromising your application or
angularapp    | 2020-09-29T13:19:50.000226100Z computer. Using a different host than the one passed to the "--host" flag might result in
angularapp    | 2020-09-29T13:19:50.000228700Z websocket connection issues. You might need to use "--disableHostCheck" if that's the
angularapp    | 2020-09-29T13:19:50.000231000Z case.
angularapp    | 2020-09-29T13:19:50.000709400Z WARNING: Running a server with --disable-host-check is a security risk. See https://medium.com/webpack/webpack-dev-server-middleware-security-issues-1489d950874a for more information.
angularapp    | 2020-09-29T13:20:10.731395000Z
angularapp    | 2020-09-29T13:20:10.731441600Z chunk {main} main.js, main.js.map (main) 57.7 kB [initial] [rendered]
angularapp    | 2020-09-29T13:20:10.731452000Z chunk {polyfills} polyfills.js, polyfills.js.map (polyfills) 141 kB [initial] [rendered]
angularapp    | 2020-09-29T13:20:10.731457200Z chunk {runtime} runtime.js, runtime.js.map (runtime) 6.15 kB [entry] [rendered]
angularapp    | 2020-09-29T13:20:10.731461400Z chunk {styles} styles.js, styles.js.map (styles) 12.7 kB [initial] [rendered]
angularapp    | 2020-09-29T13:20:10.731464600Z chunk {vendor} vendor.js, vendor.js.map (vendor) 2.71 MB [initial] [rendered]
angularapp    | 2020-09-29T13:20:10.731468000Z Date: 2020-09-29T13:20:10.730Z - Hash: 71f01e77608773fbaa56 - Time: 11928ms
angularapp    | 2020-09-29T13:20:10.732294000Z ** Angular Live Development Server is listening on 0.0.0.0:4242, open your browser on http://localhost:4242/ **
angularapp    | 2020-09-29T13:20:10.732720500Z : Compiled successfully.

这是容器的第二次运行的输出,我猜第一次编译需要更长的时间。如果你看一下第5行,它花费了超过一分钟的时间来进行初始化,并开始编译。

我看了看可能的原因,最常见的建议是增加node的内存限制。我在compose文件中通过环境变量做到了这一点。

编辑:这似乎是Windows、WSL2和Docker卷的问题。我读到了在WSL2下从容器到Windows主机的文件系统访问速度很慢的问题。在Ubuntu 20.04下的云VSP中运行该项目,并且Angular开发服务器在云资源允许的情况下尽可能快地旋转。

EN

回答 1

Stack Overflow用户

发布于 2020-12-21 06:29:18

这不是角度本身的问题。

/mnt上具有文件系统性能的WSL2 has an issue

您可以做的一件事就是向Exclusion for Windows Defender添加项目和node流程。这可能会有一点帮助,但不要期望太高。

现在,虽然WSL的性能问题还没有解决,但我建议在本地使用gitbash运行开发,只在生产环境中使用docker。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64121119

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档