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?
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 <none>:<none>
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.
0 Comments