[英]How to cache node_modules on Docker build?
I've been trying for some time to cache node_modules
on a Docker build.我一直在尝试在 Docker 构建上缓存
node_modules
一段时间。 I've tried several approaches including the one here , but without success.我尝试了几种方法,包括这里的一种,但没有成功。
My main reason to cache is because it takes 30+ minutes to build my image, which is way too much.我缓存的主要原因是因为构建我的图像需要30 多分钟,这太多了。
My Dockerfile
:我的
Dockerfile
:
# This image will be based on the oficial nodejs docker image
FROM node:4.2.1
RUN npm install -g jspm@0.17.0-beta.7 && \
npm install -g gulp && \
npm install -g tsd
# Use changes to package.json to force Docker not to use the cache
# when we change our application's nodejs dependencies:
ADD package.json /src/package.json
RUN cd /src && npm install
# Put all our code inside that directory that lives in the container
ADD . /src
# Set in what directory commands will run
WORKDIR /src
# Install dependencies
RUN cd /src && \
tsd reinstall -so && \
jspm install && \
gulp build -p
# Tell Docker we are going to use this port
EXPOSE 3000
# The command to run our app when the container is run
CMD ["npm", "run", "start-production"]
I do not have a .dockerignore
file.我没有
.dockerignore
文件。 I added one before but it still didn't cache my node_modules
.我之前添加了一个,但它仍然没有缓存我的
node_modules
。
So, how to I cache my node_modules ?那么,如何缓存我的 node_modules ? Feel free to suggest modifications to the
Dockerfile
.随意建议修改
Dockerfile
。
Thanks!谢谢!
I'm not sure whether it is the root of the error, but try to specify the destination folder in the ADD command and not the destination file .我不确定它是否是错误的根源,但尝试在ADD命令中指定目标文件夹而不是目标文件。
ADD package.json /src
Moreover, you can use COPY instead of ADD ( ADD can work with url and archives but you don't need it here).此外,您可以使用COPY而不是ADD ( ADD可以与 url 和档案一起使用,但您在这里不需要它)。
You can also specify your working directory earlier in the file.您还可以在文件的前面指定您的工作目录。
Try with this code :尝试使用此代码:
# This image will be based on the official nodejs docker image
FROM node:4.2.1
RUN npm install -g jspm@0.17.0-beta.7 && \
npm install -g gulp && \
npm install -g tsd
# Set in what directory commands will run
WORKDIR /src
# Use changes to package.json to force Docker not to use the cache
# when we change our application’s nodejs dependencies:
COPY package.json ./
RUN npm install
# Put all our code inside that directory that lives in the container
COPY . ./
# Install dependencies
RUN tsd reinstall -so && \
jspm install && \
gulp build -p
# Tell Docker we are going to use this port
EXPOSE 3000
# The command to run our app when the container is run
CMD ["npm", "run", "start-production"]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.