请注意,这将使缓存失效all该行之后的 Dockerfile 指令。这是要求在1996年 https://github.com/docker/docker/issues/1996(尚未实施,现已(2021 年)关闭),以及问题 42799 https://github.com/moby/moby/issues/42799(由ub-marco https://stackoverflow.com/users/2285820/ub-marco in 评论 https://stackoverflow.com/questions/37013947/force-a-docker-build-to-rebuild-a-single-step/37018862#comment122401447_37018862).
The 目前的解决方法是 https://github.com/docker/docker/issues/1996#issuecomment-172606763:
FROM foo
ARG CACHE_DATE=2016-01-01
<your command without cache>
docker build --build-arg CACHE_DATE=$(date) ....
这将使缓存失效ARG CACHE_DATE
每个构建的行。
初级acdc https://stackoverflow.com/users/1850609/acdcjunior报告于评论 https://stackoverflow.com/questions/37013947/force-a-docker-build-to-rebuild-a-single-step/37018862?noredirect=1#comment114435305_37018862必须使用:
docker build --build-arg CACHE_DATE=$(date +%Y-%m-%d_%H:%M:%S)
另一种解决方法 https://github.com/moby/moby/issues/42799#issuecomment-1006325840 from azul https://github.com/azul:
如果 git 发生更改,我将使用以下内容在 CI 中重建:
export LAST_SERVER_COMMIT=`git ls-remote $REPO "refs/heads/$BRANCH" | grep -o "^\S\+"`
docker build --build-arg LAST_SERVER_COMMIT="$LAST_SERVER_COMMIT"
然后在 Dockerfile 中:
ARG LAST_SERVER_COMMIT
RUN git clone ...
如果 git 存储库实际发生更改,这只会重建以下层。