docker - 强制 docker 一步构建到 'rebuild'

标签 docker

我知道 docker 有一个 --no-cache=true 选项来强制干净地构建 docker 镜像。然而,对我来说,我真正想做的就是强制最后一步在我的 dockerfile 中运行,这是一个运行 shell 脚本的 CMD 命令。

无论出于何种原因,当我修改该脚本并保存它时,典型的 docker 构建将重用该步骤的缓存版本。有没有办法强制 docker 只在这一部分上不这样做?

最佳答案

请注意,这将使该行之后的所有 Dockerfile 指令的缓存失效。这是在 Issue 1996 中请求的(尚未实现,现已(2021)关闭),以及 issue 42799 (由 ub-marcothe comments 中提及)。

current workaround is :

FROM foo
ARG CACHE_DATE=2016-01-01
<your command without cache>

docker build --build-arg CACHE_DATE=$(date) ....

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

acdcjunior报告于the comments必须使用:

docker build --build-arg CACHE_DATE=$(date +%Y-%m-%d_%H:%M:%S)
<小时/>

Another workaround来自azul :

Here's what I am using to rebuild in CI if changes in git happened:

export LAST_SERVER_COMMIT=`git ls-remote $REPO "refs/heads/$BRANCH" | grep -o "^\S\+"`
docker build --build-arg LAST_SERVER_COMMIT="$LAST_SERVER_COMMIT"

And then in the Dockerfile:

ARG LAST_SERVER_COMMIT
RUN git clone ...

This will only rebuild the following layers if the git repo actually changed.

关于docker - 强制 docker 一步构建到 'rebuild',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37013947/

相关文章:

c++ - Docker不启动cpp应用程序

docker - 具有/Terraform Digitalocean负载平衡的Docker Swarm模式。如何自动缩放?

python - 创建在 python virtualenv 中执行命令的 docker 镜像

postgresql - Docker Postgres 12 'PANIC: could not locate a valid checkpoint record'

c++ - 构建 docker 镜像时 CMake 错误 : The source directory "/" does not appear to contain CMakeLists. txt

amazon-web-services - 如何使用 Amazon ECS 从 Tutum 私有(private)注册表中提取 Docker 镜像?

docker - 如何理解Docker构建过程提供的输出?

docker - Keycloak 8 : User with username 'admin' already added

docker - 显示UJ的Dockers Cassandra节点

docker - Docker使用FROM获得先前的镜像