Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent Dockerfile caching git clone

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?

like image 366
Raindy Avatar asked May 03 '16 05:05

Raindy


People also ask

How do I stop Docker caching?

Disabling caching You can do so by passing two arguments to docker build : --pull : This pulls the latest version of the base Docker image, instead of using the locally cached one. --no-cache : This ensures all additional layers in the Dockerfile get rebuilt from scratch, instead of relying on the layer cache.

How do I create a Dockerfile without cache?

If you do not want to use the cache at all, you can use the --no-cache=true option on the docker build command.

Does GitHub cache Docker images?

Docker build-with-cache action. This action builds your docker image and caches the stages (supports multi-stage builds) to improve building times in subsequent builds.

What is Workdir in Dockerfile?

The WORKDIR command is used to define the working directory of a Docker container at any given time. The command is specified in the Dockerfile. Any RUN , CMD , ADD , COPY , or ENTRYPOINT command will be executed in the specified working directory.


2 Answers

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.

ADD https://api.github.com/repos/$USER/$REPO/git/refs/heads/$BRANCH version.json RUN git clone -b $BRANCH https://github.com/$USER/$REPO.git $GIT_HOME/ 

The API call will return different results when the head changes, invalidating the docker 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:

ADD https://$ACCESS_TOKEN:[email protected]/repos/$USER/$REPO/git/refs/heads/$BRANCH version.json 

(thx @captnolimar for a suggested edit to clarify authentication)

like image 179
anq Avatar answered Sep 29 '22 08:09

anq


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.

like image 29
VonC Avatar answered Sep 29 '22 08:09

VonC