I have written a piece of code in Python for removing the docker images which are not tagged only:
# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
release 3.1 b6bf9d19cc6c 5 hours ago 869MB
release 3.2 b6bf9d19cc6c 5 hours ago 869MB
<none> <none> 3dfdfcb0769d 6 hours ago 433MB
<none> <none> d505190470fd 6 hours ago 433MB
<none> <none> 979a42368814 7 hours ago 433MB
<none> <none> f8bcf895ffce 7 hours ago 433MB
<none> <none> 8c1ed97822da 7 hours ago 433MB
release 1.0 36e9ea407082 7 hours ago 433MB
ubuntu 16.04 6a2f32de169d 6 days ago 117MB
vault latest 144fecac962b 3 weeks ago 64.4MB
Python code:
import subprocess
output = subprocess.Popen(["docker", "images"], stdout=subprocess.PIPE)
result = output.communicate()[0].split("\n")
image_list = []
for line in result[1:]:
if ("<none>" == line.split(" ")[0]):
image_list.append(filter(None,line.split(" "))[2])
for image in image_list:
#Piping both stdout and stderr to stdout.
output = subprocess.Popen(["docker", "rmi", "-f", image], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
print(output.communicate()[0])
Final output:
docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
release 3.1 b6bf9d19cc6c 5 hours ago 869MB
release 3.2 b6bf9d19cc6c 5 hours ago 869MB
release 1.0 36e9ea407082 7 hours ago 433MB
ubuntu 16.04 6a2f32de169d 6 days ago 117MB
vault latest 144fecac962b 3 weeks ago 64.4MB
Please let me know if the code can be optimized or can be written in a more pythonic way.
docker rmi $(docker images | grep "^<none>" | awk "{print $3}"). \$\endgroup\$