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 snapshot:
........
RUN git clone --depth=1 git-repository-url $GIT_HOME/
RUN mvn package -Dmaven.test.skip
........
I want the docker do not cache the step of RUN git clone --depth=1 git-repository-url $GIT_HOME/
so that the on-going updated on the the repository can be reflected on the Docker image building. Is it possible to a achieve that?
If you use github you can use github API to not cache specific RUN command. You need to have jq installed to parse JSON: apt-get install -y jq
Example:
In Dockerfile (ARG command should be right before RUN):
or if you don't want to install jq
If repository has new commits, git clone will be executed.
I ran into this same issue myself, and I just decided to use the
--no-cache
option when I build the image, rather than trying to single out the git repo.Another workaround:
If you use github (or gitlab or bitbucket too most likely) you can ADD the github API's representation of your repo to a dummy location.
The api call will return different results when the head changes, invalidating docker's cache.
If you're dealing with private repos you can use github's
x-oauth-basic
authentication scheme with a personal access token like so:(thx @captnolimar for a suggested edit to clarify authentication)
Issue 1996 is not yet available, but you have the following workaround:
That would invalidate cache after the
ARG CACHE_DATE
line for every build.Or:
That would also invalidate cache after this ADD line.
Similar idea:
For github private repos, you could also pass in your username and password:
RUN git clone -b$BRANCH https://$USER:$PASSWORD@github.com/$USER/$REPO.git $GIT_HOME/