241

I have a few RUN commands in my Dockerfile that I would like to run with -no-cache each time I build a Docker image.

I understand the docker build --no-cache will disable caching for the entire Dockerfile.

Is it possible to disable cache for a specific RUN command?

4
  • 2
    Once you disable the cache for a single command, if the result doesn't match past cached run, you'd need to rebuild all remaining steps. Is that your goal, or do you hope to only rebuild a single layer and somehow inject that into where prior cached data was stored?
    – BMitch
    Jun 13, 2016 at 20:35
  • 7
    I was hoping to rebuild specific layers, for example a "git pull" command. Right now the "git pull" command will be cached, even though the repo is updated.
    – Vingtoft
    Jun 14, 2016 at 7:31
  • 3
    It's easy enough to force a pull by passing an unused argument. But the result of that cached entry being rebuilt is that all following layers will need a rebuild. See my answer over here for an example.
    – BMitch
    Jun 14, 2016 at 11:56
  • If looking to invalidate the cache when a git remote has changed take a look at: How to prevent Dockerfile caching git clone. All credit to @anq for the linked answer.
    – hpgmiskin
    Sep 9, 2020 at 8:06

9 Answers 9

172

There's always an option to insert some meaningless and cheap-to-run command before the region you want to disable cache for.

As proposed in this issue comment, one can add a build argument block (name can be arbitrary):

ARG CACHEBUST=1 

before such region, and modify its value each run by adding --build-arg CACHEBUST=$(date +%s) as a docker build argument (value can also be arbitrary, here it is current datetime, to ensure its uniqueness across runs).

This will, of course, disable cache for all following blocks too, as hash sum of the intermediate image will be different, which makes truly selective cache disabling a non-trivial problem, taking into account how docker currently works.

8
  • 5
    Doesn't seem to work anymore, just got ---> Using cache under my ``ARG CACHEBUST=1` line... (and yes I did do --build-arg CACHEBUST=$(date +%s) in my docker command)
    – Pylinux
    Jul 22, 2019 at 4:51
  • Does not work for me either, maybe it is platform dependent. I would have expected any ARG change to invalidate the cache.
    – Oliver
    Mar 31, 2020 at 16:23
  • 29
    You have to add RUN echo "$CACHEBUST" as just using ARG will not invalidate the cache
    – Sidharth V
    Apr 30, 2020 at 15:31
  • 4
    Confirm working as of Podman 3.2.2 and Docker 20.10.6. Side note: please do not assign the default value as in ARG CACHEBUST=1, instead just write ARG CACHEBUST. The difference is that in later case, may you forget to pass the --build-arg, you'll get a warning WARN[0000] missing "CACHEBUST" build argument. […] At least Podman behaves this way.
    – Hi-Angel
    Aug 25, 2021 at 8:58
  • 1
    It is also possible to define a label instead of the echo, i.e. LABEL cachebust=$CACHEBUST. Better choices could be of course buildDate or buildNumber or something else providing valuable meta data.
    – B. Baron
    Jun 15, 2022 at 11:36
76

Use

ADD "https://www.random.org/cgi-bin/randbyte?nbytes=10&format=h" skipcache

before the RUN line you want to always run. This works because ADD will always fetch the file/URL and the above URL generates random data on each request, Docker then compares the result to see if it can use the cache.

I have also tested this and works nicely since it does not require any additional Docker command line arguments and also works from a Docker-compose.yaml file :)

15
  • 20
    whats going to happen if random.org decides to change that endpoint? how would you control that behaviour? Jun 12, 2020 at 2:42
  • 6
    This failed some times... when site is down!!! I think it's not perfect solution for this. ADD failed: failed to GET random.org/cgi-bin/randbyte?nbytes=10&format=h with status 503 Service Unavailable: <!DOCTYPE HTML>
    – Kathi
    Jun 22, 2020 at 5:07
  • 7
    random.org has added DDOS protection which breaks this solution now
    – Brad Root
    Jun 22, 2020 at 20:58
  • 4
    not sure why noone highlighted yet, but this does add a layer with a useless skipcache file. using ARG CACHEBUST + RUN echo $CACHEBUST does not Sep 4, 2021 at 0:00
  • 5
    why don't people use a local source of randomness like /dev/urandom ???
    – jaksco
    Oct 25, 2022 at 17:00
29

If your goal is to include the latest code from Github (or similar), one can use the Github API (or equivalent) to fetch information about the latest commit using an ADD command.
docker build will always fetch an URL from an ADD command, and if the response is different from the one received last time docker build ran, it will not use the subsequent cached layers.

eg.

ADD "https://api.github.com/repos/username/repo_name/commits?per_page=1" latest_commit
RUN curl -sLO "https://github.com/username/repo_name/archive/main.zip" && unzip main.zip
2
  • 2
    This is great. When the code changed, my git clone and tests run; when the code has not changed, cached values are used. Perfection.
    – n13
    Aug 10, 2022 at 4:37
  • This should be the accepted solution, very clean and simple, just include the ADD step before whatever you currently do to install the package. Please upvote this answer to raise attention to it. Apr 24 at 16:10
17

Building on @Vladislav’s solution above I used in my Dockerfile

ARG CACHEBUST=0

to invalidate the build cache from hereon.

However, instead of passing a date or some other random value, I call

docker build --build-arg CACHEBUST=`git rev-parse ${GITHUB_REF}` ...

where GITHUB_REF is a branch name (e.g. main) whose latest commit hash is used. That means that docker’s build cache is being invalidated only if the branch from which I build the image has had commits since the last run of docker build.

11

As of February 2016 it is not possible.

The feature has been requested at GitHub

9

Not directly but you can divide your Dockerfile in several parts, build an image, then FROM thisimage at the beginning of the next Dockerfile, and build the image with or without caching

1
  • 2
    Will this enable updating the commited layers in the base docker image?
    – user_mda
    Jan 9, 2017 at 17:24
8

the feature added a week ago.

ARG FOO=bar

FROM something
RUN echo "this won't be affected if the value of FOO changes"
ARG FOO
RUN echo "this step will be executed again if the value of FOO changes"

FROM something-else
RUN echo "this won't be affected because this stage doesn't use the FOO build-arg"

https://github.com/moby/moby/issues/1996#issuecomment-550020843

5

I believe that this is a slight improvement on @steve's answer, above:

RUN git clone https://sdk.ghwl;erjnv;wekrv;[email protected]/your_name/your_repository.git

WORKDIR your_repository

# Calls for a random number to break the cahing of the git clone
# (https://stackoverflow.com/questions/35134713/disable-cache-for-specific-run-commands/58801213#58801213)
ADD "https://www.random.org/cgi-bin/randbyte?nbytes=10&format=h" skipcache
RUN git pull

This uses the Docker cache of the git clone, but then runs an uncached update of the repository.

It appears to work, and it is faster - but many thanks to @steve for providing the underlying principles.

1
  • 2
    Nice. Keep in mind this breaks when there are rewrites
    – sehe
    Feb 10, 2021 at 0:20
-5

Another quick hack is to write some random bytes before your command

RUN head -c 5 /dev/random > random_bytes && <run your command>

writes out 5 random bytes which will force a cache miss

1
  • 16
    The result of writing those random bytes gets cached as well, so if no files have changed before that command, it won't run the command again. This doesn't solve anything. Jun 24, 2019 at 13:31

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.