1

I have a docker file looks like this:

.
.
WORKING /app
COPY ..
RUN npm install
.
.

It works well but takes lots of time to be built every time (over 7 mins). After some research, I understood I have to optimize the Dockerfile so that it uses Cache. So, I've written it like this:

.
.
WORKING /app
COPY package*.json .
RUN npm install
COPY ..
.
.

Now, it builds much faster (less than 1 min). My question is "why exactly"?


If Linux can understand a file is either modified or not while copying it to either override it or read it from the cache, then I expected that both COPY .. and COPY package*.json . have the same behavior. Why only COPY package*.json . uses cache?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
stack
  • 10,280
  • 19
  • 65
  • 117

1 Answers1

1

Every line in your docker file results in a new 'layer'. Docker will keep all these layers. If nothing changed in your directory, it doesn't need to run COPY . . because it saved the layer from the last time it ran.

RUN npm install takes a while, but it only results in a new state if your dependencies changed. (you installed a new package, or updated one).

If COPY . . needs to re-run (because you updated your code), every line under there also needs to re-run. Because you likely change your code more often than your dependencies, it makes sense to install dependencies earlier.

If do you ever add a new npm package, you will see that Docker will re-run every line from RUN npm install onward.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Evert
  • 93,428
  • 18
  • 118
  • 189