How to clear Docker cache and save disk space
You can spin up a Docker container in a few seconds. A quick “docker run” command, and voila, your shiny new app is online. But this simplicity often leads to a performance bottleneck – High Disk Space usage.
Docker focuses on fast container deployment, and doesn’t focus much on disk space management. That’s why manual intervention is required to manage disk space in Docker machines.
A Docker machine can host multiple containers, each created with an image. The Docker images are downloaded and created using the instructions provided in a Dockerfile.
If not managed properly, these images can take up your disk space and crash the server. In our Docker Management Services, we help online businesses ensure high service uptime and performance.
Today, we’ll take a look at how Docker cache buildup can be prevented, and cleaned up regularly.
What is Docker cache?
Whenever a container is created for the first time, its corresponding Dockerfile is also executed for the first time. This creates a new Docker image, which is stored in the folder ‘/var/lib/docker’ by default.
During further runs of Dockerfile, Docker will create and commit a new layer to the already existing image. These images are stored in the cache.
Unless explicitly instructed, Docker always looks for an existing image in its cache, whenever a new container is created. Reusing images from the cache helps to speed up the container deployment process in Docker.
Is Docker cache problematic?
While Docker cache helps speed up container creation process, there are certain situations where using the Docker cache will fail the purpose.
For instance, in the case of a source code checkout, we need the latest code to be available. In such cases, we can specifically give instructions not to use the cache.
This is referred to as busting the cache. The Dockerfile command in such cases would look like ‘docker build –no-cache=true’.
Another major concern is that the Docker images in the cache take up disk space. The size of a Docker image is the total space taken up by the image and all its parent images.
In the long run, you will find your Docker machine running out of disk space, as a result of all the Docker images here and there. So, its important to clear the cache regularly.
[ Are your containers crying over space constraints? Our Docker experts take care of your infrastructure and ensure adequate space for smooth functioning. ]