How to get rid of Docker images with no repository?

I had dozens of Docker containers and images running on my host machine. I played around those images for my internal project work. I pushed it to my Docker Hub accounts. Now when I tried stopping the containers and removing the images I could see that lots of images with no actual repository.

Example:

root@dell-virtual-machine:~# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED                  VIRTUAL SIZE
<none>              <none>              337e820ac065     Less than a second ago   773.6 MB

<none>              <none>              8ffe63c7fe0c        20 minutes ago    655.7 MB
<none>              <none>              05713d75925a     20 hours ago       655.7 MB
<none>              <none>              e2204761e2d2     23 hours ago       655.7 MB
<none>              <none>              9d9bb03c1b31     23 hours ago       655.7 MB
<none>              <none>              0c085c5f4d90      23 hours ago       655.7 MB
<none>              <none>              ee0efbd04005      23 hours ago       547 MB
<none>              <none>              61dcf760957b      23 hours ago       300.3 MB
<none>              <none>              21abc21cba16      23 hours ago       300.3 MB
<none>              <none>              5cff31633fd1        23 hours ago       276.5 MB
<none>              <none>              919267d9932f      24 hours ago       276.5 MB
<none>              <none>              97793a6d1adf      24 hours ago       276.5 MB
<none>              <none>              fab5a70dd326      25 hours ago       276.5 MB
<none>              <none>              68798be9802c     25 hours ago       276.5 MB
<none>              <none>              052b05ba5e5b     25 hours ago       276.5 MB
<none>              <none>              2870051ec7a2     25 hours ago       276.5 MB

Let’s pick up the first image. While I supply rmi -f option with docker it threw the error as shown below:

root@dell-virtual-machine:~# docker rmi -f 337e820ac065
Error response from daemon: No such id: d121b6e6dba451bd3f1ecd3292404e6747dd2a7a1994a0e1c34644e1547245be

Now this might make you go crazy. To get rid of this image, here is the workaround:

root@dell-virtual-machine:~# docker ps -a | awk ‘{print $1}’ | grep -v CONTAINER | xargs sudo docker rm
62affdcf2be4
root@dell-virtual-machine:~# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED                  VIRTUAL SIZE
<none>              <none>              337e820ac065        Less than a second ago   773.6 MB
root@dell-virtual-machine:~# docker images | grep “<none>” | awk ‘{print $3}’ | xargs sudo docker rmi
Deleted: 337e820ac065da28cd36fbfdfb5d7dc3d02045a5abd468ce66553ea96f363a3e
Deleted: bfb56f437936c4031d752586b7157161fd68d4fda19dee5b31c156a52e3cadf7
Deleted: dbf87b16c95b22c9a25c73a5dd98e82fb7e896cfa658d669011366c31a05b35b
Deleted: 539c0211cd76cdeaedbecf9f023ef774612e331137ce7ebe4ae1b61088e7edbe

root@dell-virtual-machine:~# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
root@dell-virtual-machine:~#

Hurray !!! You have now a clean docker repository in your local environment.