4

I run a docker build with command

sudo docker build -t catskills-xview2-0.0.0 .

I have previously built this tag. It has a lot of cached build steps, like this:

Step 15/20 : RUN pip3 install matplotlib tqdm libtiff scipy Pillow scikit-image opencv-python imgaug IPython geopandas keras imantics simplification scikit-learn chainer tensorboard tensorboardX
 ---> Using cache
 ---> 2af652c17995
Step 16/20 : RUN git clone https://github.com/xview2/xview2-baseline.git ~/code/xview-2
 ---> Using cache
 ---> 8ea290c99ee8

Most of the time I want the cache. If I've updated source for a particular cache item, I want to delete that item so it will fetch it from scratch. For example, cache item 8ea290c99ee8.

I know that I can completely rebuild with --no_cache. This is slow.

Question: How do I delete a single cache item using it's key, so that the rebuild will re-fetch on that step?

Lars Ericson
  • 1,952
  • 4
  • 32
  • 45
  • Also consider running the `git clone` step outside of the Dockerfile, and `COPY`ing its content in. This will invalidate the cache only if the source tree has changed (that is, when you've checked out some other commit or updated). – David Maze Dec 06 '19 at 00:36
  • @DavidMaze, the question you cited is about preventing cacheing. I'm not trying to prevent cacheing. In general I like cacheing. Sometimes when I do a build though I may want to invalidate starting from a particular step. I assumed that's why they printed the cache IDs out. If there's no use for those IDs, they shouldn't bother printing them. – Lars Ericson Dec 06 '19 at 03:58
  • You can delete specific cache item by ID like this: `docker builder prune --filter id=8ea290c99ee8` – daymansiege Jan 04 '22 at 11:03

1 Answers1

1

No, there is no out of the box way to invalidate the cache for a particular step, but you can control this with the help of build time ARG.

Also note that if you invalidate cache for example at Step 15/20 this, then cache for the rest of the step from 15-20 all will be invalidated.

One way to deal at STEP level, you can try something

FROM node:alpine
ARG STEP1=true 
RUN apk add --no-cache npm 
ARG STEP2=true
RUN apk add --no-cache curl
ARG STEP3=true
RUN apk add --no-cache bash

So this will keep cache until I explicitly override the value of desired steps.

For example, I want to invalidate cache for step3, this only clear cache for step3.

docker build --build-arg STEP3=false -t test-cache .

but if I run this for step2, it will also clear cache for step2 and step3.

docker build --build-arg STEP2=false -t test-cache .

Adiii
  • 54,482
  • 7
  • 145
  • 148