0

I build a docker image containing the Foo package, which is built from its master version pulled from its git repo:

RUN git clone git://my.git.server/Foo.git
RUN (configure, build and install Foo)

Every time the image is built I'd like the Docker build cache to be invalidated if a new commit has been pushed into the Foo repository, in order to always build the latest version. On the contrary, if the master version is the same as the last time the image was built then the build cache should be used to avoid an unnecessary rebuild.

In its current form, my Dockerfile never needs to be changed so the build cache is never invalidated. Given my poor skills with Docker I can't figure out a possible modification to achieve my desired behavior, so I'd need help with this. Thanks in advance.

Edit: as suggested in the linked question the desired behavior can be achieved with a trick: modifying the Dockerfile as

ARG FOO_MASTER_COMMIT
RUN git clone git://my.git.server/Foo.git
RUN (configure, build and install Foo)

and then launching the build as:

git clone git://my.git.server/Foo.git
DOCKER_BUILDKIT=1 docker build --build-arg FOO_MASTER_COMMIT=$(git -C Foo rev-parse HEAD) -t myimage:ver .

does the job.

Nicola Mori
  • 777
  • 1
  • 5
  • 19
  • 1
    Does this answer your question? [How to prevent Dockerfile caching git clone](https://stackoverflow.com/questions/36996046/how-to-prevent-dockerfile-caching-git-clone) – Carson Mar 10 '21 at 14:34
  • Yes, I'd say it does. By adding an ARG to the Dockerfile and setting its value to the commit hash of the master branch when invoking docker build it works as expected. – Nicola Mori Mar 10 '21 at 14:52
  • `git ls-remote https://github.com/gturri/dokuJClient.git | grep "HEAD" | grep -oE "[0-9a-f]+"` is a helpful snippet if you want to get a remote branches hash without pulling it down – Carson Mar 10 '21 at 15:20
  • 1
    Wow, I didn't know ls-remote. Very useful indeed, thanks again! – Nicola Mori Mar 10 '21 at 16:47

0 Answers0