8

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 <reharik@gmail.com>

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 <reharik@gmail.com>
 ---> 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 MF_Domain@0.0.1 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.

Henrik Sachse
  • 51,228
  • 7
  • 46
  • 59
Raif
  • 8,641
  • 13
  • 45
  • 56

1 Answers1

4

from the docker documentation it is said that

In the case of the ADD and COPY 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.

Community
  • 1
  • 1
Thomasleveil
  • 95,867
  • 15
  • 119
  • 113