如何防止Dockerfile缓存git clone

Rai*_*ndy 32 git docker dockerfile

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

........
RUN git clone --depth=1 git-repository-url $GIT_HOME/
RUN mvn package -Dmaven.test.skip
........
Run Code Online (Sandbox Code Playgroud)

我希望docker不要缓存步骤,RUN git clone --depth=1 git-repository-url $GIT_HOME/以便可以在Docker镜像构建上反映存储库上正在进行的更新.是否有可能实现这一目标?

anq*_*anq 36

另一种解决方法:

如果您使用github(或者最有可能使用gitlab或bitbucket),您可以将github API的repo表示添加到虚拟位置.

   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/
Run Code Online (Sandbox Code Playgroud)

当磁头改变时,api调用将返回不同的结果,使docker的缓存无效.

如果您正在处理私有存储库,您可以使用github的x-oauth-basic身份验证方案个人访问令牌,如下所示:

   ADD https://$ACCESS_TOKEN:x-oauth-basic@api.github.com/repos/$USER/$REPO/git/refs/heads/$BRANCH version.json
Run Code Online (Sandbox Code Playgroud)

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

  • 不错的解决方案。继承人如何做到这一点。`添加https:// [USER]:[PASS] @ api.bitbucket.org / 2.0 / repositories / [ORG-NAME] / [REPO-NAME] / commit / [BRANCH] / info``运行git clone-深度1-分支[分支] https:// [USER]:[PASS] @ bitbucket.org / [ORG-NAME] / [REPO-NAME] .git / repo`我建议创建一个特定的“集成”用户,并使用“应用密码”进行位存储桶。https://confluence.atlassian.com/bitbucket/app-passwords-828781300.html将此与多阶段构建结合起来,到目前为止,它似乎是完美的解决方案。 (2认同)
  • 请以完全通用的独立于提供商的方式进行操作,这里有一个选项“ADD http://worldtimeapi.org/api/ip /time.tmp” (2认同)
  • 与 2020 年一样,github api 似乎发生了变化,并且 URL `https://api.github.com/repos/$USER/$REPO/git/refs/heads/$BRANCH` 不再起作用,因为API更新?请建议更新后的网址。 (2认同)

tom*_*h99 14

我自己也遇到了同样的问题,我只是决定--no-cache在构建映像时使用该选项,而不是尝试挑出 git repo。

docker build --no-cache -t my_image .
Run Code Online (Sandbox Code Playgroud)


Von*_*onC 13

1996年的问题尚未公布,但您有以下解决方法:

FROM foo
ARG CACHE_DATE=2016-01-01
RUN git clone ...

docker build --build-arg CACHE_DATE=$(date) ....
Run Code Online (Sandbox Code Playgroud)

这将使ARG CACHE_DATE每个构建的行之后的缓存无效.

要么:

ADD http://www.convert-unix-time.com/api?timestamp=now /tmp/bustcache
RUN git pull
Run Code Online (Sandbox Code Playgroud)

这也会在此ADD行之后使缓存无效.

类似的想法:

ARG命令添加到Dockerfile:

# Dockerfile
# add this and below command will run without cache
ARG CACHEBUST=1
Run Code Online (Sandbox Code Playgroud)

当您需要使用选定的缓存重建时,请使用--build-argoption 运行它

$ docker build -t your-image --build-arg CACHEBUST=$(date +%s) .
Run Code Online (Sandbox Code Playgroud)

然后只有ARGDockerfile 中的命令下面的层将重建.


Waz*_*ime 9

对于任何有 Gitlab 存储库问题的人:

Gitlab 在调用他们的 API 时有这个烦人的分支 id 方法,该 ID 将出现在你的存储库名称下 在此输入图像描述

# this will copy the last change from your brach and it'll invalidate the cache if there was a new change
ADD "https://gitlab.com/api/v4/projects/${PROJECT_ID}/repository/branches/master?private_token=${GIT_TOKEN}" /tmp/devalidateCache

# the actual clone
RUN git clone --depth=1 https://${GIT_USER}:${GIT_TOKEN}@gitlab.com/${git_file_uri} ${BASE_BUILD_PATH}
Run Code Online (Sandbox Code Playgroud)


Mic*_*d a 6

如果您使用 github,您可以使用 github API 来不缓存特定的 RUN 命令。您需要安装 jq 来解析 JSON: apt-get install -y jq

例子:

docker build --build-arg SHA=$(curl -s 'https://api.github.com/repos/Tencent/mars/commits' | jq -r '.[0].sha') -t imageName .
Run Code Online (Sandbox Code Playgroud)

在 Dockerfile 中(ARG 命令应该在 RUN 之前):

ARG SHA=LATEST
RUN SHA=${SHA} \
    git clone https://github.com/Tencent/mars.git
Run Code Online (Sandbox Code Playgroud)

或者如果你不想安装 jq

SHA=$(curl -s 'https://api.github.com/repos/Tencent/mars/commits' | grep sha | head -1)
Run Code Online (Sandbox Code Playgroud)

如果存储库有新的提交, git clone 将被执行。