如何在monorepo项目中使用docker-compose处理node_modules
我正在使用纱线工作区运行 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, -c, "/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负责安装所有应用程序的依赖项。 - 我必须为每个应用程序创建一个卷 + 为根 node_modules 创建一个卷
- 整个项目安装在每个服务中,即使我只使用一个特定的文件夹
我想避免为开发而构建,因为每次添加依赖项时都必须进行构建,这非常麻烦并且会减慢您的速度
THE END
二维码