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用于建议编辑以澄清身份验证)
tom*_*h99 14
我自己也遇到了同样的问题,我只是决定--no-cache在构建映像时使用该选项,而不是尝试挑出 git repo。
docker build --no-cache -t my_image .
Run Code Online (Sandbox Code Playgroud)
Von*_*onC 13
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:Run Code Online (Sandbox Code Playgroud)# Dockerfile # add this and below command will run without cache ARG CACHEBUST=1当您需要使用选定的缓存重建时,请使用
--build-argoption 运行它Run Code Online (Sandbox Code Playgroud)$ docker build -t your-image --build-arg CACHEBUST=$(date +%s) .然后只有
ARGDockerfile 中的命令下面的层将重建.
对于任何有 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)
如果您使用 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 将被执行。
| 归档时间: |
|
| 查看次数: |
9240 次 |
| 最近记录: |