Clear Docker Buildx Cache

You might have encountered a situation where you cannot build the latest Docker image when using the Buildx. If so, you may find this article helpful to give you a little insight into your question, “Why I keep seeing the stale image in the list!”.

What I tried was building the Docker image supporting ARM64 architecture. To achieve my goal, it requires me to enable Buildx experimental feature of Docker. It allows us to build a Docker image supporting multiple architectures. When I have found a problem in the image, I wanted to change the Dockerfile to reflect the fix I applied. But it failed. As shown in the following output, the CREATED time of the images keeps the past time even I have created just a few seconds before.

$ docker images
REPOSITORY                                                    TAG                      IMAGE ID            CREATED             SIZE
presto                                                        341-SNAPSHOT             c15822305160        5 hours ago         1.05GB
lewuathe/presto-worker                                        341-SNAPSHOT             eb1d11521b04        5 hours ago         1.38GB
lewuathe/presto-coordinator                                   341-SNAPSHOT             8e0085374165        5 hours ago         1.38GB

That’s was so annoying that I could not test my fix was adequately resolving the issue. Here are two options to overcome this stressful situation.

Build without any cache

As well as normal build command, buildx also provides --no-cache option. It enables us to build an image from scratch. The latest image will be created for sure.

$ docker buildx build \
    --no-cache \ # Without using cache
    --platform linux/arm64/v8 \
    -f Dockerfile-arm64v8 \
    -t lewuathe/prestobase:340-SNAPSHOT-arm64v8

Clearing the cache completely

Another option is clearing the cache. However, it has a side-effect affecting other image build time. Since removing all layer caches, it can make the build time for other images longer. But if the images you are holding is not so many, deleting the cache can be a reasonable option.

The builder instance holds the cache. The following command will clear the cache hold by all builders.

$ docker builder prune --all

Afterward, you can build the image as usual. We can see the build time is refreshed as follows.

$ docker images
REPOSITORY                                                    TAG                      IMAGE ID            CREATED             SIZE
presto                                                        341-SNAPSHOT             c15822305160        a second ago        1.05GB
lewuathe/presto-worker                                        341-SNAPSHOT             eb1d11521b04        a second ago        1.38GB
lewuathe/presto-coordinator                                   341-SNAPSHOT             8e0085374165        a second ago        1.38GB

To learn the practical techniques of Docker, you may find the following guide from Manning useful. Docker has many options or configurations. If you know these details, Docker will be more attentive tool for you.

Thanks for reading as usual!

References