微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在 monorepo 项目中使用 docker-compose 处理 node_modules

如何解决如何在 monorepo 项目中使用 docker-compose 处理 node_modules

我正在使用 yarn 工作区运行 Node.js monorepo 项目。 文件结构如下所示:

workspace_root
    node_modules
    package.json
    apps
        appA
            node_modules
            package.json
        appB
            node_modules
            package.json
    libs
        libA
            dist
            node_modules
            package.json

所有应用都是独立的,但它们都需要 libA

我正在使用 docker-compose 运行所有这些应用程序。我的问题是如何正确处理所有依赖项,因为我不希望 node_modules 文件夹与主机同步。 在本地,当我在工作区根目录运行 yarn install 时,它会安装所有项目的所有依赖项,填充不同的 node_modules。 在 docker-compose 中,理想情况下,每个应用都不应该知道其他应用。

到目前为止,我的方法行之有效,但并不理想且可扩展性不强。

version: "3.4"

services:
  # The core is in charge of installing dependencies for ALL services. Each service must for wait the core,and then
  # just do their job,not having to handle install.
  appA:
    image: node:14-alpine
    volumes: # We must load every volumes for install
        - .:/app  # Mount the whole workspace structure
        - root_node_modules:/app/node_modules
        - appA_node_modules:/app/apps/appA/node_modules
        - appB_node_modules:/app/apps/appB/node_modules
        - libA_node_modules:/app/libs/libA/node_modules
    working_dir: /app/apps/appA
    command: [sh,-c,"yarn install && yarn run start"]

  appB:
    image: node:14-alpine
    volumes: # We must load every volumes for install
        - .:/app  # Mount the whole workspace structure
        - root_node_modules:/app/node_modules
        - appB_node_modules:/app/apps/appB/node_modules
    working_dir: /app/apps/appB
    command: [sh,"/scripts/wait-for-it.sh appA:4001  -- yarn run start"]

    # And so on for all apps....
  
volumes:
    root_node_modules:
        driver: local
    appA_node_modules:
        driver: local
    appB_node_modules:
        driver: local
    libA_node_modules:
        driver: local

我看到的主要缺点:

  • 服务 appA 负责安装所有应用的依赖项。
  • 我必须为每个应用程序创建一个卷 + 为根节点模块创建一个
  • 整个项目都安装在每个服务中,即使我只使用一个特定的文件

我想避免为开发而构建,因为每次添加依赖项时都必须进行构建,这非常麻烦并且会拖慢您的速度

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。