Prevent caching steps when building a docker image
To prevent docker from caching when building a new image, you can simple use the --no-cache
option of the
docker build
command.
But mostly this should be done earliest at a specific step, for instance when copying files from an url or package manager.
In such case simply pass a build argument with a changing or random value, such as local time or a random string.
All steps in the same stage of the dockerfile following the first step using this argument, will become invalid.
In this example I copy a cacheable conan configuration into the image.
The gtest installation should be for a version better than 1.10 and not beeing cached.
Dockerfile:
FROM conanio/conan:latest as conan
ARG CACHE_DIS=1
ENV CONAN_NON_INTERACTIVE=1
COPY ./conan_config /tmp/conan_config
RUN conan config install /tmp/conan_config
# starting with the next line, the cache is invalidated and all steps will be re-executed
RUN echo "$CACHE_DIS"
RUN conan install "gtest/[>1.10]@" --build=all
To build this file, simply pass a random number (like $RANDOM
from bash):
$ docker build --build-arg CACHE_DIS=$RANDOM ...