How To Clear the Docker cache for a clean Docker restart

How To Clear the Docker cache for a clean Docker restart?

To cleanly restart the Docker, you are required to clear the Docker cache by removing containers, images, network, volumes, and build layers, and by pruning the Docker system. The Docker Desktop application can also be used to clear the Docker cache.

What is Docker?

Docker is a well-established and rapidly growing technology that is utilized to dockerize user applications and software in isolated packages known as containers. The Docker containers are one of the major components that are managed by the Docker host. It encapsulates the application with its source code, dependencies, and essential configuration settings. These containers are further created through images. 

The images are blueprints or snapshots of containers that contain instructions on how to dockerize the application. Moving forward, to preserve the container data outside the container, the Docker volumes are used. These all components are used by docker to generate, deploy, and ship applications and software smoothly without any trouble.

However, while building or using Docker environments, users may face performance or space issues. As Docker images and other components take a huge amount of system resources, they can affect the system’s performance. To cleanly restart Docker by decluttering the Docker environment, users need to clear the Docker cache.

How to Remove Docker Cache?

To clean up the Docker environment either on Windows, Linux, or MacOS, the Docker CLI tool is used. The commands and steps will remain the same for all major OS to remove the cache. However, users can also remove Docker cache from Docker Desktop applications. In this section, we will remove the Docker cache through the Docker CLI tool by following the below-listed step-by-step procedure.

  • Remove Docker Containers
  • Remove Docker Images
  • Remove Docker Volumes
  • Remove Docker Networks
  • Remove Builder Cache
  • System Prune

So, let’s get started!

Remove Docker Containers

Docker containers use OS virtualization to generate, run, and deploy applications in a Docker-isolated environment. To deploy applications error-free on any system, these encapsulate the application along with essential packages. Due to this, containers may take a lot of system resources such as CPU, RAM, and memory. To free up the space to improve the system performance, users need to remove these containers.

To remove the Docker containers to declutter the Docker environment, follow the below instructions.

Step 1: View Disk Space Usage

First, check the space used by Docker Daemon to manage the Docker components. To do so, simply run the “docker system df” command:

docker system df

From the output, you can see images and containers take up more system space. Moreover, there is one dangling volume that exists and the Docker build cache also takes “436B” system space:

docker system df

To clear the Docker cache, users need to clear all these spaces by removing Docker components.

Step 2: Stop all Containers

To remove containers from Docker, users first need to stop or kill the running containers. To do so, execute the below command:

docker stop $(docker ps -a -q)

The above command is a combination of two different docker commands. The first part will stop the container that is accessed and listed by the second part of the command “docker ps”. Here, “-a -q” options are used to list all containers by id:

docker stop $(docker ps -a -q)

Step 3: Remove All Containers

To remove Docker containers, execute the below command:

docker rm $(docker ps -a -q)

Here, again first command “docker rm” will remove all the containers that are accessed by the “docker ps -a -q” command:

docker rm $(docker ps -a -q)

Step 4: Prune the Docker Containers

However, the above-given command will remove all the containers. In case, if any ambiguity exists, it will be removed by the “docker container prune” command:

docker container prune -f

The “-f” option will remove the container forcefully without showing any warning or confirmation text:

docker container prune -f

Note: Before removing the containers, if any essential container data is required, must create a backup of your containers. 

Remove Docker Images

Docker images are snapshots of containers that contain instructions to build and manage applications inside the container. These are usually huge inside as these images consist of build layers and containers with all the required dependencies, base container image, user program, and application. To clean up the Docker cache, users need to remove the container images through the following steps.

Step 1: Remove All Images

First, remove all the docker images through the following command:

docker rmi $(docker images -a -q)

The above command is composed of two different commands. The “docker rmi” command removes the images and the “docker images -a -q” command will access the images by id:

docker rmi $(docker images -a -q)

Note: At that point, the user may encounter an error, if any image is being used by the Docker container. In this situation, utilize the “-f” option with the “docker rmi” command to forcefully remove the images:

docker rmi -f $(docker images -a -q)

It is also suggested to remove the docker containers before removing images to reduce the chance of any error occurrence.

Step 2: Prune Docker Images

To remove only dangling and unused images from Docker, the user can use the “docker image prune” command:

docker image prune

The warning will display on the terminal console, press “Y” to remove all dangling images:

docker image prune

Remove Docker Volumes

Docker volume is an external file system that is used to persist the data of a container. If a container is removed, that container data will be saved in volume for later use. It is mostly created for backup purposes, and it is managed by Docker host. To clean up the Docker cache to cleanly restart Docker, remove the Docker volume through the given steps.

Step 1: List Docker Volume

List down the Docker volume and check if any essential volume is required or not:

docker volume ls

Here, only the “demo” volume exists:

docker volume ls

Step 2: Prune All Docker Volumes

To prune or remove all volumes from Docker, simply run the “docker volume prune” command:

docker volume prune -a

The “-a” option will be used to remove all volumes from the Docker host:

docker volume prune -a

Remove Builder Cache

When the user creates the container blueprint, the builder cache saves the build layers from previous builds to speed up the build process. To clear the builder cache to remove these saved build layers, simply prune the builder through the “docker builder prune” command:

docker builder prune
docker builder prune

System Prune

To completely clean the Docker development environment by removing the Docker cache and Docker component, simply prune the Docker system using the below command:

docker system prune -a --volumes

In the given command, “–volumes” is used to prune the Docker volumes along with other Docker components:

docker system prune -a --volumes

For verification, again check the disk usage through the “docker system df” command:

docker system df

The below output shows that we have effectively cleared the Docker cache to cleanly restart Docker:

docker system df

Bonus Tip: How to Remove Docker Cache From Docker Desktop Application?

The Docker Desktop app is a GUI version of Docker that includes all essential packages and plugins. The Docker Desktop app is mostly used on Windows and MacOS. However, users can also use it on Linux as well. To clear the Docker cache from the Docker Desktop application, go through the following demonstration.

Step 1: Launch Docker Desktop

First, launch the Docker Desktop application via the Windows Start menu. On Mac, the user can launch the Docker Desktop app from the “Applications” directory:

Launch Docker Desktop

Step 2: Clean Docker Data

Next, click on the below pointed “bug” icon to open the troubleshooting option for Docker. To clean the Docker data, click on the “Clean / Purge data” button:

 Clean Docker Data

Mark the data set that you want to clean and press the “Delete” button. Here, we have removed the “Windows Containers” data:

Delete Windows Containers Data

Step 3: Factory Reset Docker

In order to completely clean the Docker, the user can factory reset the Docker by hitting the “Reset to factory defaults” button:

Factory Reset Docker

This will pop up a confirmation box, press the “Yes, reset anyway” to clean the Docker:

Reset Docker Desktop to factory defaults

We have covered the methods to clear the Docker Cache using Docker CLI and the Docker Desktop app.

Conclusion

To clear the Docker cache through Docker CLI, first, remove the Docker containers, images, volume, and builder cache. After that, prune the Docker system using the “docker system prune -a –volumes” command. To remove the Docker cache from the Desktop application, simply open the troubleshooting setting and factory reset the Docker. This post has provided the approaches to remove the Docker cache to cleanly restart the Docker.

Similar Posts