I can't seem to make my dockerfile cache my npm install. I have it set up like all the examples specify, and the package.json doesn't change but it still downloads all the dependencies.
Here's what I have
FROM mf/nodebox
# Maintainer
MAINTAINER Raif Harik <[email protected]>
RUN rm /bin/sh && ln -s /bin/bash /bin/sh
ADD /app/package.json /tmp/package.json
RUN cd /tmp && npm install && npm install -g babel
RUN cd /tmp && cp -a /tmp/node_modules /opt/app/current/node_modules
# Entrypoint to docker shell
ENTRYPOINT ["docker-shell"]
#this is the flag that tells the docker-shell what mode to execute
# Startup commands
CMD ["-r"]
# set WORKDIR
WORKDIR /opt/app/current
# Add shell script for starting container
ADD ./docker-shell.sh /usr/bin/docker-shell
RUN chmod +x /usr/bin/docker-shell
COPY /app /opt/app/current
Then the output I get is
Building domain...
Step 0 : FROM mf/nodebox
---> 4ee7c51a410d
Step 1 : MAINTAINER Raif Harik <[email protected]>
---> Using cache
---> 78d0db67240c
Step 2 : RUN rm /bin/sh && ln -s /bin/bash /bin/sh
---> Using cache
---> d7d360d8f89a
Step 3 : ADD /app/package.json /tmp/package.json
---> 5b373dae5141
Removing intermediate container f037272f49c3
Step 4 : RUN cd /tmp && npm install && npm install -g babel
---> Running in cb89bb6fc2d0
npm WARN package.json [email protected] No description
So it's caching the first couple commands, but it stops at Step 3 the ADD package.json and then goes to npm for Step 4.
Edit:
I guess i should mention that when I deploy a new change in the code ( or for my experimenting with this issue, just the same code ), while the package.json stays the same it is copies over to the deploy folder. I don't know if docker checks the createddate, the checksum, or does a diff. If it's the createddate then maybe that's the issue.
Because building images is a common task, Docker provides several tools that speed up builds. The most important feature for improving build speeds is Docker's build cache.
--no-cache - This will force rebuilding of layers already available. --pull - This will trigger a pull of the base image referenced using FROM ensuring you got the latest version.
from the docker documentation it is said that
In the case of the
ADD
andCOPY
instructions, the contents of the file(s) being put into the image are examined. Specifically, a checksum is done of the file(s) and then that checksum is used during the cache lookup. If anything has changed in the file(s), including its metadata, then the cache is invalidated.
Those metadata include the file modification time.
There are tricks to get around this (for instance docker add cache when git checkout same file).
See also the related discussion on the Docker github project.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With