docker add cache when git checkout same file

前端 未结 2 823
失恋的感觉
失恋的感觉 2020-12-16 00:42

I need checkout project in my CI server build image reusing docker cache.

Docker ADD not use cache when checkout same file.

I am in git branch A

相关标签:
2条回答
  • 2020-12-16 01:21

    Docker invalidates the docker build cache when a file's mtime value has changed, and git does not track file mtime values. This cache invalidation also shows up in other situations too, like in continuous integration or build environments involving docker, git and branches.

    I have been using a "touch" target in a Makefile that I run prior to asking docker to build a container:

    touch:
        @echo "Reset timestamps on git working directory files..."
        find ./ | grep -v .git | xargs touch -t 200001010000.00
    

    Next, always run make touch prior to docker build or any docker-based "build" target in the same Makefile...

    Another option is to setup a git hook that modifies mtime values automatically: https://git.wiki.kernel.org/index.php/ExampleScripts#Setting_the_timestamps_of_the_files_to_the_commit_timestamp_of_the_commit_which_last_touched_them

    Another possible solution is to fork docker and remove mtime from its definition of caches: https://github.com/docker/docker/blob/master/pkg/tarsum/tarsum.go

    Note: as of docker 1.8, mtime is no longer taken into account when invalidating the cache. Pull request #12031 updated this behavior

    0 讨论(0)
  • 2020-12-16 01:28

    git checkout, git clone, git fetch, an so on modify the creation date of file. Then docker see another file even is the same.

    docker ADD command fails caching when creation date of file change.

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