How to prevent Dockerfile caching git clone

后端 未结 7 580
忘了有多久
忘了有多久 2020-11-30 08:19

I have a Dockerfile trying to package and deploy a web app to a container. The code of app fetches from git repository during Docker image building. Here\'s the Dockerfile s

7条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-30 09:00

    Issue 1996 is not yet available, but you have the following workaround:

    FROM foo
    ARG CACHE_DATE=2016-01-01
    RUN git clone ...
    
    docker build --build-arg CACHE_DATE=$(date) ....
    

    That would invalidate cache after the ARG CACHE_DATE line for every build.

    Or:

    ADD http://www.convert-unix-time.com/api?timestamp=now /tmp/bustcache
    RUN git pull
    

    That would also invalidate cache after this ADD line.

    Similar idea:

    Add ARG command to your Dockerfile:

    # Dockerfile
    # add this and below command will run without cache
    ARG CACHEBUST=1
    

    When you need to rebuild with selected cache, run it with --build-arg option

    $ docker build -t your-image --build-arg CACHEBUST=$(date +%s) .
    

    then only layer below ARG command in Dockerfile will rebuild.

提交回复
热议问题