Clearing Unnamed Docker Images

I’ve been playing around with Docker a lot lately and the number of images on my host keeps climbing. In doing so, I’ve managed to end up with a number of images which weren’t properly named/tagged and I really have no idea what they’re for at this point. Oh, sure, I could always just do a [shell]docker inspect image_id[/shell] on the image and see what it is, but I’m lazy. It’s easier just to get rid of them. 🙂

So if I use ‘docker images’ to see what I have available you can see I’ve got a number of images in the state I describe.

REPOSITORY                         TAG                 IMAGE ID            CREATED             SIZE
runlevl4/couch                     latest              c0d143528267        19 hours ago        573MB
runlevl4/openjdk                   latest              1a04169be1f0        20 hours ago        738MB
runlevl4/alpine-wso2               latest              382b74b4df93        11 days ago         757MB
<none>                             <none>              2fbbf4f7f2ae        11 days ago         757MB
runlevl4/alpine-wso2               v1                  581ac302a5e7        11 days ago         757MB
<none>                             <none>              3c2bd15fa447        11 days ago         757MB
centos7/nonroot                    v1                  03f554e67899        13 days ago         197MB
<none>                             <none>              e876350b9c92        13 days ago         197MB

Here’s a quick and easy way to get rid of them. First, we’ll do a dry run and just return the images we want to purge.

docker images | grep "<none>" | tr -s " " | cut -d " " -f 3

docker images – returns all the images on our host
grep “” – reduces the results to just those we’re after
tr -s ” ” – this is a fun one. the -s argument tells tr to replace all of the spaces with a single space
cut -d ” ” -f 3 – we use cut to reduce the results to just the image id. -d sets the delimiter as a single space and -f grabs the third field

Let’s see some examples as we build the command out.

docker images | grep "<none>"
<none>                             <none>              2fbbf4f7f2ae        11 days ago         757MB
<none>                             <none>              3c2bd15fa447        11 days ago         757MB
<none>                             <none>              e876350b9c92        13 days ago         197MB

Now we throw in the tr command.

docker images | grep "<none>" | tr -s " "
<none> <none> 2fbbf4f7f2ae 11 days ago 757MB
<none> <none> 3c2bd15fa447 11 days ago 757MB
<none> <none> e876350b9c92 13 days ago 197MB

And finally, we return just the image id.

docker images | grep "<none>" | tr -s " " | cut -d " " -f 3
2fbbf4f7f2ae
3c2bd15fa447
e876350b9c92

Now that we know our command is working, we can pass it as an argument to the [shell]docker rmi[/shell] command. ‘docker rmi’ is the CLI command to remove one or more Docker images.

docker rmi $(docker images | grep "<none>" | tr -s " " | cut -d " " -f 3)
Deleted: sha256:2fbbf4f7f2aef4d47d74a44ec32fe2cf8d3fd4650b7d216675908f3757651b21
Deleted: sha256:3c2bd15fa447d9e5ffd12e8d149147fb5dd66cb48c9f152748b42514abcaf5a1
Deleted: sha256:84243c17c3cf36e418afe44880b33490f537e91b81ef9ac78a3d345e6a7609b0
Deleted: sha256:e876350b9c92fce722c096ef989216f802b264276676219cc2f321ff279d5ba5

Now you can use [shell]docker images[/shell] again to confirm that they’re gone.

REPOSITORY                         TAG                 IMAGE ID            CREATED             SIZE
runlevl4/couch                     latest              c0d143528267        19 hours ago        573MB
runlevl4/openjdk                   latest              1a04169be1f0        20 hours ago        738MB
runlevl4/alpine-wso2               latest              382b74b4df93        11 days ago         757MB
runlevl4/alpine-wso2               v1                  581ac302a5e7        11 days ago         757MB
centos7/nonroot                    v1                  03f554e67899        13 days ago         197MB

Post to Twitter

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.