We live in an era where storage is becoming cheaper everyday. We can just send everything to the cloud and pay almost nothing.
So why would we need to worry about deleting Docker images?
First of all, there are still some mission critical workloads that can’t be moved to the cloud, especially those in heavily regulated industries like law or healthcare.
But to better answer that question, I would say that we as developers often find ourselves out of space on our local machines.
Let’s do a quick analysis of this StackOverflow public dataset to explore that further:
SELECT tag,
title,
answer_count,
favorite_count,
score,
view_count VIEWS
FROM
(SELECT title,
answer_count,
favorite_count,
view_count,
score,
SPLIT(tags, '|') tags
FROM `bigquery-public-data.stackoverflow.posts_questions`
posts_questions), UNNEST(tags) tag
WHERE tag = 'docker'
AND title LIKE '%space left%'
ORDER BY VIEWS DESC
Query Results:
So it does’t happen just with me, right? Look at how many views we have on those StackOverflow posts. If you are wondering, the number is 465687 views for posts matching the search query.
Luckily for us, today we are going to see some easy-to-use examples on how to delete our dangling and unused docker images to help ourselves out.
What is the difference between dangling and unused images, you might ask?
A dangling image means that you’ve created a new build of the image but haven’t given it a new name. Think about those old, forgotten images that no one knows what to do with anymore – those are “dangling images”.
They are left untagged and display <none>
on their name when you run docker images
.
On the other hand, an unused image means that it has not been assigned or is not being used in a container.
For example, when running docker ps -a
– it will list all your currently running containers plus exited containers. Any images being used inside any of containers are shown as “used images”, and any others are unused.
Now let’s see some examples of how to delete Docker images.
The Busy Cat Corp is a fictional company that captures cat behavior data, and provides recommendations to cat owners on how to make their pets busier and happier.
All their workloads are containerized, and they use the following database images:
cassandra, postgres, mysql and mongo.
Their developers are constantly running out of space on their machines, and they are top users of StackOverflow – aren’t we all?
So they asked us for some quick examples of how to delete some images and get their space back.
First let’s take a look at the machine of one of their developers.
docker images
Output
REPOSITORY TAG IMAGE ID CREATED SIZE
<none> <none> 9c872a6119cc About a minute ago 384MB
mysql latest 5ac22cccc3ae 43 hours ago 544MB
cassandra 3 9fab0c92a93d 4 days ago 384MB
adoptopenjdk 8-jre... 2bf0172ac69b 4 days ago 210MB
mongo latest 6d11486a97a7 2 weeks ago 388MB
postgres latest b97bae343e06 6 weeks ago 313MB
That’s cool, they have all the images from their workloads downloaded. But look at the disk space – it’s more than 2GB! Let’s see what we can do for them.
We’ll start by looking for dangling images.
#docker #devops