Reputation: 100060
I want to manually cache node_modules
using Docker, something like this:
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
this works, but it appears to me that /tmp/test-deps/node_modules
is re-created each time the container is built.
How can I create a persistent directory so that I don't have to re-install node_modules each time?
Ridiculously difficult to find information on how to cache anything in any directory w/ Docker.
Upvotes: 1
Views: 232
Reputation: 100060
It's counterintuitive, because Docker handles caching its own way - but this seems to work for me:
https://blog.playmoweb.com/speed-up-your-builds-with-docker-cache-bfed14c051bf
The bad way (Docker cannot do caching for you):
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"]
With a small change, we can give Docker the ability to cache things for us!
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
Upvotes: 1