How can I prevent a Dockerfile instruction from being cached?

后端 未结 3 1633
無奈伤痛
無奈伤痛 2020-12-14 00:51

In my Dockerfile I use curl or ADD to download the latest version of an archive like:

FROM debian:jessie
...
RUN apt-g         


        
相关标签:
3条回答
  • 2020-12-14 00:54

    A build-time argument can be specified to forcibly break the cache from that step onwards. For example, in your Dockerfile, put

    ARG CACHE_DATE=not_a_date
    

    and then give this argument a fresh value on every new build. The best, of course, is the timestamp.

    docker build --build-arg CACHE_DATE=$(date +%Y-%m-%d:%H:%M:%S) ...
    

    Make sure the value is a string without any spaces, otherwise docker client will falsely take it as multiple arguments.

    See a detailed discussion on Issue 22832.

    0 讨论(0)
  • 2020-12-14 01:09

    add && exit 0 after a command will invalidate the cache from there.

    Example:

    RUN apt-get install -y unzip && exit 0

    0 讨论(0)
  • 2020-12-14 01:12

    docker build --no-cache would invalidate the cache for all the commands.

    Dockerfile ADD command used to have the cache invalidated. Although it has been improved in recent docker version:

    Docker is supposed to checksum any file added through ADDand then decide if it should use the cache or not.

    So if the file added has changed, the cache should be invalidated for the ADD command.


    Issue 1326 mentions other tips:

    This worked.

    RUN yum -y install firefox #redo
    

    So it looks like Docker will re-run the step (and all the steps below it) if the string I am passing to RUN command changes in anyway - even it's just a comment.

    The docker cache is used only, and only if none of his ancestor has changed (this behavior makes sense, as the next command will add change to the previous layer).

    The cache is used if there isn't any character which has changed (so even a space is enough to invalidate a cache).

    0 讨论(0)
提交回复
热议问题