加速 NPM 在 Docker 容器中的安装 [英] Speed up NPM install in Docker container

查看:64
本文介绍了加速 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:

=> http://bitjudo.com/blog/2014/03/13/building-efficient-dockerfiles-node-dot-js/

推荐答案

这个方法很神奇:

https://blog.playmoweb.com/使用 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 容器中的安装的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆