首页 > 解决方案 > Docker 容器启动 monorepo 应用程序很慢

问题描述

当我运行 docker-compose 通过 NX 构建 Angular 和 Nest 应用程序的 monorepo 时,构建速度很快,但容器运行速度非常慢。但如果我直接在我的 Mac 上运行这些应用程序,它的构建速度会非常快。我希望应用程序在 docker 中构建得更快。

在我的 docker-compose.yml 中有一个服务示例:

  dollop-accounts-web:
    command: npm start dollop-accounts-web
    build:
      args:
        - NODE_VERSION=${NODE_VERSION}
        - PORT=${ACCOUNTS_WEB_PORT}
      context: .
    container_name: dollop-accounts-web
    image: dollop-node-image
    ports:
      - ${ACCOUNTS_WEB_PORT}:${ACCOUNTS_WEB_PORT}
    volumes:
      - .:/usr/src/app

这是我用于所有服务的 dockerfile(请注意,我为每个容器复制了整个 monorepo,因为我不知道如何更好地处理它):

ARG NODE_VERSION
ARG PORT
FROM node:$NODE_VERSION
WORKDIR /usr/src/app
COPY . .
EXPOSE $PORT

我运行 7 个这样的服务,并且我有一个 Nest 应用程序的日志:

> dollop@1.0.0 start /usr/src/app
> nx serve "dollop-accounts-service"


> nx run dollop-accounts-service:serve 
Starting type checking service...
Using 2 workers with 2048MB memory limit

而Angular App的这种日志:

> dollop@1.0.0 start /usr/src/app
> nx serve "dollop-tasks-web"


> nx run dollop-tasks-web:serve 
WARNING: This is a simple server for use in testing or debugging Angular applications
locally. It hasn't been reviewed for security issues.

Binding this server to an open connection can result in compromising your application or
computer. Using a different host than the one passed to the "--host" flag might result in
websocket connection issues. You might need to use "--disableHostCheck" if that's the
case.
Another process, with id 35, is currently running ngcc.
Waiting up to 250s for it to finish.
(If you are sure no ngcc process is running then you should delete the lock-file at /usr/src/app/node_modules/@angular/compiler-cli/ngcc/__ngcc_lock_file__.)

如您所见,一次启动所有应用程序很困难。但即使只有一项服务,它也比我在主机上运行时要慢得多。有没有办法优化这个?

标签: angulardockernestjsmonoreponrwl-nx

解决方案


推荐阅读