3

Here is my Dockerfile

FROM ubuntu:18.04
RUN apt-get update -y
RUN apt-get upgrade -y
RUN apt-get install mariadb-common mariadb-server mariadb-client nodejs npm -y
RUN git clone https://github.com/yigalirani/sqlrabbit.js.git && cd sqlrabbit.js && npm install 

Note that the last line clones a git repo.

My question is: when calling docker build . , is there a way to force docker to check the latest commit of the git repo and rebuild the layer if needed?

yigal
  • 3,923
  • 8
  • 37
  • 59

1 Answers1

2

That was discussed in this thread (git clone vs.copy) and this blog post (using wget)

But to avoid git clone caching, you will need to ADD a file whose content will change when a new commit is done.

anq suggests:

ADD https://api.github.com/repos/$USER/$REPO/git/refs/heads/$BRANCH version.json

That uses the GitHub refs API.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • yes, that seems to solve the problem, but it look like a hack. it would be nice of the docker folks to solve it more elegantly. how about a new 'GIT' instruction in the dockerfile? – yigal Jul 09 '19 at 05:38
  • @yigal It is not an hack, but a cache-busting technique common in Dockerfile. A Git instruction would be a local command which would need to come *after* the git clone, so too late. The GitHub API call can be done *before* the clone. – VonC Jul 09 '19 at 05:55
  • yes, I understand and will use it in my project. it would be nice though if the docker system would do it automatically. instead of doing `RUN git clone ` do a `GIT `. basically I am suggesting a new instruction that is like 'add' and 'copy' but for git repos – yigal Jul 09 '19 at 06:15
  • @yigal That is a good idea! I am not sure though the Dockerfile syntax accepts new commands. It seems kind of frozen in term of API/commands, which might be a good thing. – VonC Jul 09 '19 at 06:53
  • only works for github though.. right? – Duane Jan 15 '23 at 21:19
  • 1
    @Duane True, this is using the GitHub API and would not work for another Git repositories hosting service. Note that the recent BuildKit (0.11.0) and Dockerfile 1.5.0 allow to copy/clone a repository (without the history) or just a file of a repository, with the [new `ADD git-ref` command](https://stackoverflow.com/a/75101766/6309). And that is *not* limited to GitHub. – VonC Jan 15 '23 at 21:41