如何防止 Dockerfile 缓存 git clone [英] How to prevent Dockerfile caching git clone
问题描述
我有一个 Dockerfile,试图将 Web 应用程序打包并部署到容器中.应用程序的代码在 Docker 镜像构建期间从 git 存储库中获取.这是 Dockerfile 快照:
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
........
我希望 docker 不缓存 RUN git clone --depth=1 git-repository-url $GIT_HOME/
的步骤,以便可以反映存储库上正在进行的更新关于 Docker 镜像构建.有可能实现吗?
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?
推荐答案
另一种解决方法:
如果您使用 GitHub(或 gitlab 或 bitbucket 也很可能),您可以将 GitHub API 的存储库表示添加到虚拟位置.
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/
当head改变时API调用会返回不同的结果,使docker缓存失效.
The API call will return different results when the head changes, invalidating the docker cache.
如果您正在处理私人存储库,您可以使用 github 的 x-oauth-basic
认证方案 带有个人访问令牌 像这样:
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
(感谢@captnolimar 的建议编辑以澄清身份验证)
(thx @captnolimar for a suggested edit to clarify authentication)
这篇关于如何防止 Dockerfile 缓存 git clone的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!