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
- Method 2: Remove Cached Docker Layers by Removing all Docker Build Cache Using “docker builder prune”
- Method 3: Remove Cached Docker Layers by Removing the Docker Image
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:
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:
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:
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:
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:
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.