加快NPM在Docker容器中的安装

编程入门 行业动态 更新时间:2024-10-11 21:27:48
本文介绍了加快NPM在Docker容器中的安装的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我们使用的标准做法是在版本控制中不包括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容器中的安装

本文发布于:2023-10-29 00:20:06,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1538181.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:容器   NPM   Docker

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!