Docker撰写了名为Volume&的v3版本来自npm install的node_modules [英] Docker compose v3 named volume & node_modules from npm install
问题描述
使用compose v3。
Using compose v3.
在构建中,我复制package.json并运行npm install到
In the build I copy package.json and run npm install into
/var/www/project/node_modules
我在构建阶段不添加任何代码。
I dont add any code in the build phase.
在撰写中我添加了卷
- ./www:/var/www/project/www
众所周知,主机绑定到/ www会有效地覆盖我在构建阶段安装的node_modules。
As everyone knows the host bind to /www will effectively "overwrite" the node_modules I installed during the build phase.
这就是为什么我们随后添加命名模块的原因
Which is why we add a named module afterwards
- ./www:/var/www/project/www
- modules:/var/www/project/www/node_modules
这在我们第一次构建/运行项目时很好而且很花俏
this works fine and dandy the first time we build/run the project
由于命名卷模块不存在,因此www / node_modules从构建阶段开始将被安装。
since the named volume "modules" doesnt exist, the www/node_modules from the build phase will be mounted instead.
但是,因为这是实际问题。
HOWEVER, in this is the actual issue.
下次我更改package.json并执行以下操作:
The next time I make a change to package.json and do:
docker-compose up --build
我可以看到如何安装新的npm模块,但是一旦附加了命名的模块卷(它在以前的运行中已经存在),它就会覆盖映像中新安装的模块。
I can see how the new npm modules are installed but once the named "modules" volume is attached (it now exists stuff there from the previous run) it "overwrites" the newly installed modules in the image.
上面提到的添加命名卷的方法建议以吨为单位,以解决节点模块问题。但是据我从大量测试中可以看到,这只能运行一次。
the above method of adding a named volume is suggested in tons of places as a remedy for the node modules issue. But as far as I can see from lots of testing this only works once.
如果每次我对package.json进行更改时都要重命名命名卷,那当然可以。
if I were to rename the named volume every time I make a change to package.json it would of course work.
推荐答案
更好的办法是在入口点包含 rm
命令脚本在运行 npm install
之前清理节点模块。
A better thing would be to include rm
command in your entrypoint script to clean out node modules before running npm install
.
作为替代方案,您可以使用 $ docker system prune
,然后再运行另一个构建。这样可以确保不使用任何较早的东西。
As an alternative, you can use $ docker system prune
before running another build. This will make sure that no earlier things are being used.
这篇关于Docker撰写了名为Volume&的v3版本来自npm install的node_modules的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!