jkj2000
jkj2000

Reputation: 1593

force a docker build to 'rebuild' a single step

I know docker has a --no-cache=true option to force a clean build of a docker image. For me however, all I'd really like to do is force the last step to run in my dockerfile, which is a CMD command that runs a shell script.

For whatever reason, when I modify that script and save it, a typical docker build will reuse the cached version of that step. Is there a way to force docker not to do so, just on that one portion?

Upvotes: 10

Views: 6457

Answers (1)

VonC
VonC

Reputation: 1324268

Note that this would invalidate the cache for all Dockerfile directives after that line. This is requested in Issue 1996 (not yet implemented, and now (2021) closed), and issue 42799 (mentioned by ub-marco in the comments).

The current workaround is:

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

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

That would invalidate cache after the ARG CACHE_DATE line for every build.

acdcjunior reports in the comments having to use:

docker build --build-arg CACHE_DATE=$(date +%Y-%m-%d_%H:%M:%S)

Another workaround from 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.

Upvotes: 8

Related Questions