加速 NPM 在 Docker 容器中的安装

编程入门 行业动态 更新时间:2024-10-11 17:24:58
本文介绍了加速 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 上搜索了docker cache npm dependencies",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/使用 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:33,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1538183.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:容器   NPM   Docker

发布评论

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

>www.elefans.com

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