将持久性数据存储在docker容器目录中

我想用Docker手动cachingnode_modules ,如下所示:

 COPY . . # copy everything (node_modules is gitignored though) COPY package.json /tmp/test-deps RUN (cd /tmp/test-deps && npm install --no-optional > /dev/null 2>&1) RUN ln -s /tmp/test-deps/node_modules /root/cdt-tests/node_modules 

这是有效的,但是在我看来,每次构build容器时都会重新创build/tmp/test-deps/node_modules

如何创build一个持久目录,以便我不必每次都重新安装node_modules?

可笑地很难find有关如何cachingDocker任何目录中的任何信息。

这是违反直觉的,因为Docker以自己的方式处理caching – 但这似乎适用于我:

https://blog.playmoweb.com/speed-up-your-builds-with-docker-cache-bfed14c051bf

不好的方法(Docker不能为你caching):

 FROM mhart/alpine-node WORKDIR /src # Copy your code in the docker image COPY . /src # Install your project dependencies RUN npm install # Expose the port 3000 EXPOSE 3000 # Set the default command to run when a container starts CMD ["npm", "start"] 

通过一个小小的改变,我们可以让Docker有能力为我们caching一些东西!

 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 ["npm", "start"] # Install app dependencies COPY package.json /src RUN npm install # Copy your code in the docker image COPY . /src