Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I remove a single layer from from docker to prevent caching?

I have a long-running docker build process, so I would prefer not to disable caching for the entire build (with --no-cache). However, I would like to invalidate caching for a particular step.

I had a bright idea: remove the cached layer and rebuild so this has to rebuild.

I used:

docker build --progress=plain

to get hold of the sha of the cached layer:

#16 [stage-9  3/15] RUN pip install -r /tmp/requirements.lock
#16 sha256:e4ac79a1eac5702cd296ccf33a1cfa2e0c3890c77d42737dc62a3b26ac3e798e
#16 CACHED

But then I got this error

> docker rmi e4ac79a1eac5702cd296ccf33a1cfa2e0c3890c77d42737dc62a3b26ac3e798
Error: No such image: e4ac79a1eac5702cd296ccf33a1cfa2e0c3890c77d42737dc62a3b26ac3e798

Is there an (easy) way of deleting this layer?

Note: For most use cases (and maybe even this one) you might like to use the --no-cache option for docker build

like image 771
Att Righ Avatar asked Jan 18 '26 16:01

Att Righ


1 Answers

Not directly possible. But can be done with some changes to your docker file.

To forcibly break the cache, you can use "build-time arguments" (ARG); changing the value of that argument breaks the cache, and every step after it, for example:

FROM something
RUN apt-get update && apt-get install foo bar baz ......

ARG CACHE_DATE=2016-01-01

# steps below will always be executed if `CACHE_DATE` is changed to a unique value
RUN blablabla

And set a new date for CACHE_DATE during build:

docker build --build-arg CACHE_DATE="$(date)" ....

details taken directly from this github issue.

like image 179
Just a coder Avatar answered Jan 20 '26 06:01

Just a coder



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!