Docker

How to Remove Cached Docker Layers?

Docker images are one of the core parts of the Docker platform and are also referred to as a snapshot of the Docker container. It is used to create and guide the container on how to execute the containerized program. But sometimes, while creating the Docker image from Dockerfile, Docker creates extra Docker layers or intermediate images to speed up the build process. But these layers or images may take up a lot of the user’s disk space and affect the Docker performance.

This blog will demonstrate how to remove the cached Docker layers.

How to Remove Cached Docker Layers?

Unfortunately, there is no direct way to remove cached docker layers. Many users try to remove the cached layer through the “docker system df” command. But this command will remove all the containers and images that are not related to our query. However, the user can utilize the “–no-cache” option when building the image to avoid Docker’s build cache and creates the image from the start. Another way is users can remove the intermediate image using the “docker rmi” command. Users can also utilize the “docker builder prune” command to remove Docker build cache.

To avoid or remove the Docker cached layer, the following methods can be used:

Method 1: Avoid Cached Docker Layers by Using the “–no-cache” Option

To avoid Docker’s build cache while recreating the Docker image, simply use the “–no-cache” option. This option restricts the Docker from using already cached Docker layers and creates the image from scratch. For the illustration, go through the given steps.

Step 1: View Image Build History

To access the cached layer of any Docker image or to access image history, utilize the “docker history <img-name>” command:

docker history python-img

Step 2: Use “–no-cache” Option to Avoid Cache

Next, recreate the image from scratch by avoiding Docker cached layer using the “docker build –no-cache” command:

docker build --no-cache -t python-img .

In the above command, the “–no-cache” option is specifically used to avoid the Docker cached layer:

That’s how Docker users can avoid the Docker cached layer.

Method 2: Remove Cached Docker Layers by Removing all Docker Build Cache Using “docker builder prune”

The other possible solution for removing the cached Docker layers is to remove Docker’s build cache using the “docker builder prune” command.

To do so, simply execute the “docker builder prune” command as shown below:

docker builder prune

Note that, this will remove all unused or dangling build cache of Docker:

Method 3: Remove Cached Docker Layers by Removing the Docker Image

To remove the cached Docker layer of the image, remove the whole image through the “docker rmi” command. To do so, follow the listed instructions.

Step 1: View All Images

First, list down all Docker images using the “docker images” command. Then, note the Image ID to remove the particular image from which you want to remove the cached Docker layer:

docker images

Step 2: Remove Image by ID

Next, utilize the “docker rmi <image-id>” command to remove the image. For demonstration, we have removed the dangling image:

docker rmi 868e4211d4f4

That’s all about removing the cached Docker layers.

Conclusion

Unfortunately, there is no direct way to remove cached docker layers. However, you can remove all dangling and unused build cache of Docker using the “docker builder prune” command. One other approach is to remove the Docker cached layer by removing the Docker image completely. Users can also avoid the build cache of Docker using the “–no-cache” option in the “docker build” command. This article has demonstrated the methods to remove or avoid the cached Docker layer.

About the author

Rafia Zafar

I am graduated in computer science. I am a junior technical author here and passionate about Programming and learning new technologies. I have worked in JAVA, HTML 5, CSS3, Bootstrap, and PHP.