You can use the option '
--no-cache' to instruct Docker to not use cache when building the image.
Docker help:
--no-cache Do not use cache when building the image
--no-cache-filter stringArray Do not cache specified stages
Let's use this Dockerfile:
$ vi Dockerfile
FROM ubuntu:latest
RUN groupadd -r mtitek --gid=1001 && useradd -r -g mtitek --uid=1001 mtitek
The first time we build the Dockerfile we will get the following output:
$ docker build -t ubuntu-test-no-cache:latest .
[+] Building 1.3s (7/7) FINISHED docker:default
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 133B 0.0s
=> [internal] load metadata for docker.io/library/ubuntu:latest 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [1/2] FROM docker.io/library/ubuntu:latest@sha256:6015f66923d7afbc53558d7ccffd325d43b4e249f41a6e93eef074c9505d2233 0.6s
=> => resolve docker.io/library/ubuntu:latest@sha256:6015f66923d7afbc53558d7ccffd325d43b4e249f41a6e93eef074c9505d2233 0.5s
=> [auth] library/ubuntu:pull token for registry-1.docker.io 0.0s
=> [2/2] RUN groupadd -r mtitek --gid=1001 && useradd -r -g mtitek --uid=1001 mtitek 0.3s
=> exporting to image 0.2s
=> => exporting layers 0.1s
=> => exporting manifest sha256:d29add6517c260a07d37781e4e70158572711a701f5fa40331a65191c93251dc 0.0s
=> => exporting config sha256:45a4fd31c91fcf641818459d9f29164134d1b3cbc6e1af3a05bd88cb5e6270a4 0.0s
=> => exporting attestation manifest sha256:f12ad540e9c0fa11fcaed029b79691aa4af6489b67f4e7553ba99ef58e95ca3f 0.0s
=> => exporting manifest list sha256:85e0ea95caa6802f8c74485c302e965f00f2c1a934ca6a359181c0aa60a8f281 0.0s
=> => naming to docker.io/library/ubuntu-test-no-cache:latest 0.0s
=> => unpacking to docker.io/library/ubuntu-test-no-cache:latest
If we build again the Dockerfile (with no changes) we will get the following output:
$ docker build -t ubuntu-test-no-cache:latest .
[+] Building 0.2s (6/6) FINISHED docker:default
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 133B 0.0s
=> [internal] load metadata for docker.io/library/ubuntu:latest 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [1/2] FROM docker.io/library/ubuntu:latest@sha256:6015f66923d7afbc53558d7ccffd325d43b4e249f41a6e93eef074c9505d2233 0.0s
=> => resolve docker.io/library/ubuntu:latest@sha256:6015f66923d7afbc53558d7ccffd325d43b4e249f41a6e93eef074c9505d2233 0.0s
=> CACHED [2/2] RUN groupadd -r mtitek --gid=1001 && useradd -r -g mtitek --uid=1001 mtitek 0.0s
=> exporting to image 0.1s
=> => exporting layers 0.0s
=> => exporting manifest sha256:d29add6517c260a07d37781e4e70158572711a701f5fa40331a65191c93251dc 0.0s
=> => exporting config sha256:45a4fd31c91fcf641818459d9f29164134d1b3cbc6e1af3a05bd88cb5e6270a4 0.0s
=> => exporting attestation manifest sha256:9949898eb53d54d07c3d0371e9bfdcbd52bf65d28e24154ec4d642e3fe80befa 0.0s
=> => exporting manifest list sha256:97d14803e7398851c1dcef5ab7f563a7481d3f83abaf1a65308d38d4c4e1c179 0.0s
=> => naming to docker.io/library/ubuntu-test-no-cache:latest 0.0s
=> => unpacking to docker.io/library/ubuntu-test-no-cache:latest
You can notice that the step 2 has the mention '
=> CACHED ...' which means that Docker just used the already built layer from this step.
Using local cache can speed up build time; especially when avoiding rebuilding image layers that take long time to build or take up large amounts of disk space.
In some cases (either to play safe or because we encountered some issues with the build) we might want to disable the cache, for that we use the option
--no-cache:
$ docker build --no-cache -t ubuntu-test-no-cache:latest .
[+] Building 0.7s (6/6) FINISHED docker:default
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 133B 0.0s
=> [internal] load metadata for docker.io/library/ubuntu:latest 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> CACHED [1/2] FROM docker.io/library/ubuntu:latest@sha256:6015f66923d7afbc53558d7ccffd325d43b4e249f41a6e93eef074c9505d2233 0.0s
=> => resolve docker.io/library/ubuntu:latest@sha256:6015f66923d7afbc53558d7ccffd325d43b4e249f41a6e93eef074c9505d2233 0.0s
=> [2/2] RUN groupadd -r mtitek --gid=1001 && useradd -r -g mtitek --uid=1001 mtitek 0.4s
=> exporting to image 0.2s
=> => exporting layers 0.1s
=> => exporting manifest sha256:19e6fa37eb98759c1537c4054008142026c2205a10c90945780dccbbd00c4678 0.0s
=> => exporting config sha256:745759414b5a46cb7295ca50fb37ce6c986285036bf464238354c743fe52f234 0.0s
=> => exporting attestation manifest sha256:54b9a2afd02045d1c4d8c84aee04541666464bedf9ddc0f8c60c8423ab74c6ec 0.0s
=> => exporting manifest list sha256:81f25bc23c19f2ef72d48daa0490e210f66a3c5e062a7b71ff35e57083c91249 0.0s
=> => naming to docker.io/library/ubuntu-test-no-cache:latest 0.0s
=> => unpacking to docker.io/library/ubuntu-test-no-cache:latest
You can notice that the step 2 has no cache mention which means that Docker is rebuilding again the image layer for that step.