我们使用的标准做法是在版本控制中不包括node_modules。但是,当通过CI / CD管道移动时,我们必须在多个位置重新安装NPM依赖项,这会使一切变得非常缓慢。
We use the standard practices of not including node_modules in version control. However, when moving through the CI/CD pipeline, we have to reinstall NPM dependencies in several places and it makes everything very slow.
是否可以通过某种方式用Docker缓存NPM依赖项?我搜索了Google泊坞窗缓存npm依赖项,然后从2014年的第一个命中产生了无效链接。
Is there a way to somehow cache NPM dependencies with Docker? I searched Google "docker cache npm dependencies" and the first hit from 2014 yielded a dead link.
没有其他有价值的东西了。
Nothing else of much value came up.
一种解决方案是在版本控制中包含node_modules,但是我认为这将是一个巨大的错误。我认为以某种方式缓存依赖项是最好的选择。
One solution is to include node_modules in version control, but I think that would be a huge mistake. I think caching the dependencies somehow would be the best option.
以下是Dockerfile:
Here is the Dockerfile as is:
FROM node:6 COPY . . # copy all files, but node_modules does not exist ( => gitignored) RUN npm install --no-optional --only=production > /dev/null 2>&1 RUN npm install -g bower > /dev/null 2>&1 RUN bower install --config.interactive=false --allow-root > /dev/null 2>&1 ENTRYPOINT ["/bin/bash", "/root/cdt/run.sh"]这里是一种可能的解决方案,但我不太清楚它是如何工作的:
Here is one possible solution, but I can't quite figure out how it works:
= > bitjudo/blog / 2014/03/13 / building-efficient-dockerfiles-node-dot-js /
推荐答案此方法就像魔术一样:
blog.playmoweb/speed-up-your-builds-with-docker-cache-bfed14c051bf
Docker有一种特殊的方式为您缓存内容,显然最好使用先天的缓存功能。
Docker has a special way of caching things for you, and apparently it's best to use the inborn caching ability.
不能说我完全理解它的工作原理,但它确实有效
Cannot say I completely understand how it works, but it does work.
如果遵循此模式,它将为您工作:
If you follow this pattern, it will work for you:
FROM mhart/alpine-node:5.6.0 WORKDIR /src # Expose the port 3000 EXPOSE 3000 # Set the default command to run when a container starts CMD ["node", "server.js"] # Install app dependencies COPY package.json /src RUN npm install # Copy your code in the docker image COPY . /src更多推荐
加快NPM在Docker容器中的安装
发布评论