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?

3
  • Also consider running the git clone step outside of the Dockerfile, and COPYing 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). Commented Dec 6, 2019 at 0: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. Commented Dec 6, 2019 at 3:58
  • 1
    You can delete specific cache item by ID like this: docker builder prune --filter id=8ea290c99ee8 Commented Jan 4, 2022 at 11:03

1 Answer 1

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 .

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.