How to Remove Dangling or Unused Local Docker Image using docker image prune Command in Microsoft Windows

Posted on

Introduction

Another article which is focusing on the usage of docker command. Basically, it is a docker command which is very useful in order to remove docker image. Specifically, it is the one which is in the category of a dangling docker image. In this context, there is a different between a dangling docker image and an unused docker image. The reference on the differentiation for those two types of docker images exist in this link. So, according to that reference, the simple way to define the difference is using its name. In summary, both of them is not used in any docker containers. Both of them may be freshly created images or currently do not have any docker container using them. But in the case of the unused docker image, it has the docker image name definition. On the other hand, in the case of dangling docker image, it does not have any image name definition.

For an example, the following is the list of available docker image in the local device :

C:\repository\docker>docker image list --all
REPOSITORY  TAG    IMAGE ID     CREATED      SIZE
mysql       5.7    77811e598d43 23 hours ago 462MB
wordpress   latest 4fd19a131d40 3 days ago   609MB
<none>      <none> 26fca947bf02 5 years ago  436MB

C:\repository\docker>

In the above output command execution, the first and the second line of the docker image list have their own name in the repository respectively. While in the last line, the label name is ”. Moreover, not only the name but actually the value of the tag is also ‘<none>’. In this case, to remove the dangling image, just use the ‘docker image prune’. Although, removing that image can also possible using the ‘docker image rm image_id’ command.

How to Remove Dangling or Unused Docker Image using docker image prune Command

This part is only showing how to remove or to delete the dangling docker image using either the ‘docker image prune’ or ‘docker image rm’ command. The first one is using the suitable command which has the intention to remove dangling and also the unused image which has no name as follows :

C:\repository\docker>docker image prune --all
WARNING! This will remove all images without at least one container associated to them.
Are you sure you want to continue? [y/N] y
Deleted Images:
untagged: mysql:5.7
deleted: sha256:77811e598d43121a29800e2ae03afdda569c3a7b95927631527f30c3c56492f7
untagged: wordpress:latest
deleted: sha256:4fd19a131d40eddaea3c1d0cf487f31e7f6aa59b01c5e32c1563a5574137ed35
deleted: sha256:26fca947bf02558b628824e3e38fa1c97dbfc6c4562b2be07026281ff9019509

Total reclaimed space: 0B

C:\repository\docker>docker image list --all
REPOSITORY TAG IMAGE ID CREATED SIZE

C:\repository\docker>

The second step is to remove the dangling and also unused docker image using the ‘docker image rm’. But in order to remove it all, it is necessary to specify the image id. So, all the image id must be available as the ‘docker image rm’ parameter as follows :

C:\repository\docker\wordpress>docker images -a
REPOSITORY   TAG          IMAGE ID     CREATED      SIZE
wordpress    latest       c46ad5ea445b 2 hours ago  609MB
mysql        5.7          77811e598d43 25 hours ago 462MB
<none> <none> 26fca947bf02 5 years ago  436MB

C:\repository\docker\wordpress>docker image rm $(docker images -a)
unknown shorthand flag: 'a' in -a)
See 'docker image rm --help'.

C:\repository\docker\wordpress>docker rmi $(docker image list --all)
unknown flag: --all)
See 'docker rmi --help'.

C:\repository\docker\wordpress>docker rmi $(docker image list -a)
unknown shorthand flag: 'a' in -a)
See 'docker rmi --help'.

C:\repository\docker\wordpress>docker rmi $(docker images -a)
unknown shorthand flag: 'a' in -a)
See 'docker rmi --help'.

C:\repository\docker\wordpress>docker rmi *
Error response from daemon: invalid reference format

C:\repository\docker\wordpress>docker image rm c4 77 26
Untagged: wordpress:latest
Deleted: sha256:c46ad5ea445b8640bdf9d810dfa01679159a22778cf57c64097e4139dea3943f
Untagged: mysql:5.7
Deleted: sha256:77811e598d43121a29800e2ae03afdda569c3a7b95927631527f30c3c56492f7
Deleted: sha256:26fca947bf02558b628824e3e38fa1c97dbfc6c4562b2be07026281ff9019509

C:\repository\docker\wordpress>

In the above sequence, there are several attempts to feed the ‘docker image rm’ with the result of the execution of another command. That other command is for listing the available docker images. But apparently, in Windows operating system, the result of the other command execution in ‘$(docker images -a)’ and all of those variant commands are not working. So, the above command is passing part of the image id manually. The above command execution is using two digits of the image id.

One thought on “How to Remove Dangling or Unused Local Docker Image using docker image prune Command in Microsoft Windows

Leave a Reply