我正在使用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, -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的卷
- 整个项目在每个服务中都被挂载,尽管我只使用了特定的文件夹
我想避免在开发过程中进行构建,因为每次添加一个依赖项都必须这样做,这相当繁琐且会减慢速度。