Need help?

Our experts have had an average response time of 13.14 minutes in February 2024 to fix urgent issues.

We will keep your servers stable, secure, and fast at all times for one fixed price.

How to clear Docker cache and save disk space

by | Apr 25, 2018

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.

Running out of disk space in Docker?

CLICK HERE TO FREE UP SPACE!

 

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. ]

How to remove all stopped containers

If you are no longer using some containers and they have exited running, this is how you can remove them.

First get a list of all containers using ‘docker ps -a -q’ command.

‘docker rm’ command is then used to remove all the containers that are not running (exited).

docker rm $(docker ps -q -f status=exited) 

To entirely wipe out all the Docker containers, the command used is:

docker rm $(docker ps -a -q)

What are dangling images?

Every Docker image has a repository name and tag associated with it. For instance, an Ubuntu Docker image may have ‘Ubuntu’ as the repository name and ‘latest’ as the tag.

As the name suggest, a dangling image in Docker is something that is inactive and not being used by any other running images.

The'repo:tag' for dangling images would be shown as : or untagged when the command ‘docker images’ is executed.

Since the dangling images cause wastage of disk space, they need to be deleted periodically to clear up the disk space for efficient functioning of the server.

[ Running a Docker infrastructure doesn’t have to be hard, or costly. Get world class Docker management services at affordable pricing. ]

How to remove dangling images

The command ‘docker images –filter “dangling=true”‘ will display untagged images, that are not used by any containers.

You can do a batch-cleanup of containers by listing the untagged images and deleting them, using this single command:

docker rmi $(docker images -f "dangling=true" -q)

In case any container is using that image, it would show a warning message when you try to remove it.

In short..

Manually clearing the images to create disk space every now and then, would be tiring. We automate these tasks using routine scripts.

But we do check the container contents and take necessary backups before removing them, to avoid no accidental deletion of data with the automated scripts.

Our monitoring script alerts us when the disk space goes above 75%, even after all the clean up tasks done. In such cases, we do further actions such as adding more disk space.

We also give recommendations to server owners on how to manage their server resources effectively. If you’d like to know how to get the best out of your servers, we’d be happy to talk to you.

 

 

Looking for a stable Docker setup?

Talk to our Docker specialists today to know how we can keep your containers top notch!

CLICK HERE FOR YOUR SOLUTION!

var google_conversion_label = "owonCMyG5nEQ0aD71QM";

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

Categories

Tags

Privacy Preference Center

Necessary

Necessary cookies help make a website usable by enabling basic functions like page navigation and access to secure areas of the website. The website cannot function properly without these cookies.

PHPSESSID - Preserves user session state across page requests.

gdpr[consent_types] - Used to store user consents.

gdpr[allowed_cookies] - Used to store user allowed cookies.

PHPSESSID, gdpr[consent_types], gdpr[allowed_cookies]
PHPSESSID
WHMCSpKDlPzh2chML

Statistics

Statistic cookies help website owners to understand how visitors interact with websites by collecting and reporting information anonymously.

_ga - Preserves user session state across page requests.

_gat - Used by Google Analytics to throttle request rate

_gid - Registers a unique ID that is used to generate statistical data on how you use the website.

smartlookCookie - Used to collect user device and location information of the site visitors to improve the websites User Experience.

_ga, _gat, _gid
_ga, _gat, _gid
smartlookCookie
_clck, _clsk, CLID, ANONCHK, MR, MUID, SM

Marketing

Marketing cookies are used to track visitors across websites. The intention is to display ads that are relevant and engaging for the individual user and thereby more valuable for publishers and third party advertisers.

IDE - Used by Google DoubleClick to register and report the website user's actions after viewing or clicking one of the advertiser's ads with the purpose of measuring the efficacy of an ad and to present targeted ads to the user.

test_cookie - Used to check if the user's browser supports cookies.

1P_JAR - Google cookie. These cookies are used to collect website statistics and track conversion rates.

NID - Registers a unique ID that identifies a returning user's device. The ID is used for serving ads that are most relevant to the user.

DV - Google ad personalisation

IDE, test_cookie, 1P_JAR, NID, DV, NID
IDE, test_cookie
1P_JAR, NID, DV
NID
hblid

Security

These are essential site cookies, used by the google reCAPTCHA. These cookies use an unique identifier to verify if a visitor is human or a bot.

SID, APISID, HSID, NID, PREF
SID, APISID, HSID, NID, PREF