如何防止Dockerfile缓存git克隆 [英] How to prevent Dockerfile caching git clone

查看:275
本文介绍了如何防止Dockerfile缓存git克隆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Docker文件尝试打包并部署一个Web应用程序到一个容器。在Docker图像构建期间,应用程序的代码从git存储库中获取。
这是Dockerfile快照:

  ........ 
RUN git clone - depth = 1 git-repository-url $ GIT_HOME /
运行mvn包-Dmaven.test.skip
........

我希望docker不缓存的步骤运行git clone --depth = 1 git-repository-url $ GIT_HOME / ,以便存储库上正在进行的更新可以反映在Docker映像构建中。

解决方案

另一个解决方法:



如果您使用github(或gitlab或bitbucket太可能),您可以将github API的代理代码添加到虚拟位置。

 添加https://api.github.com/repos/$USER/$REPO/git/refs/heads/$BRANCH version.json 
运行git clone -b $ BRANCH https:// github。 com / $ USER / $ REPO.git $ GIT_HOME /

api调用将返回不同的结果,当如果您正在处理私人回扣,您可以使用

auth /#basic-authenticationrel =nofollow noreferrer> github的 x-oauth-basic 认证方案,一个个人访问令牌,如下所示:

  ADD https:// $ ACCESS_TOKEN:x-oaut h-basic@api.github.com/repos/$USER/$REPO/git/refs/heads/$BRANCH version.json 

(thx @captnolimar为建议编辑以澄清身份验证)


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?

解决方案

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 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:

   ADD https://$ACCESS_TOKEN:x-oauth-basic@api.github.com/repos/$USER/$REPO/git/refs/heads/$BRANCH version.json

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

这篇关于如何防止Dockerfile缓存git克隆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆