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

时间:2017-05-04 21:58:35

标签: node.js docker dockerfile

我想使用Docker手动缓存node_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  

这样可行,但在我看来,每次构建容器时都会重新创建/tmp/test-deps/node_modules

如何创建持久性目录,以便每次都不必重新安装node_modules?

很难找到有关如何使用Docker在任何目录中缓存任何内容的信息。

1 个答案:

答案 0 :(得分:1)

这是违反直觉的,因为Docker以自己的方式处理缓存 - 但这似乎对我有用:

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

糟糕的方式(Docker无法为你做缓存):

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为我们缓存一些东西!

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
相关问题