Removing untagged docker images using Python 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
&lt;none&gt;              &lt;none&gt;              3dfdfcb0769d        6 hours ago         433MB
&lt;none&gt;              &lt;none&gt;              d505190470fd        6 hours ago         433MB
&lt;none&gt;              &lt;none&gt;              979a42368814        7 hours ago         433MB
&lt;none&gt;              &lt;none&gt;              f8bcf895ffce        7 hours ago         433MB
&lt;none&gt;              &lt;none&gt;              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 ("&lt;none&gt;" == 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.
I would split the line of the output once and re-use that. Also, if you don't pass a separator to str.split, it will split on whitespace, so "A    B C".split() == ['A', 'B', 'C'].

import subprocess

output = subprocess.Popen(["docker", "images"], stdout=subprocess.PIPE)
result = output.communicate()[0].split("\n")

image_list = []
for line in result[1:]:
    line = line.split()
    if line[0] == "&lt;none&gt;":
        image_list.append(line[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])


I would probably change if line[0] == "&lt;none&gt;" to if line[1] == "&lt;none&gt;" to adhere more closely to what you want to do. I'm not sure if it can happen that the repository is not none, but the tag is.

I would also change the grouping slightly, so that things which logically belong together are grouped. This is the first step towards putting them into their own functions, which could be slightly overkill here, but would make it a lot easier to re-use parts of this:

import subprocess


def get_docker_images():
    output = subprocess.Popen(["docker", "images"], stdout=subprocess.PIPE)
    return map(str.split, output.communicate()[0].split("\n")[1:-1])


def no_tag(images):
    for image in images:
        if image[1] == "&lt;none&gt;":
            yield image


def delete_images(images):
    for image in images:
        output = subprocess.Popen(["docker", "rmi", "-f", image[2]],
                                  stdout=subprocess.PIPE,
                                  stderr=subprocess.STDOUT)
        print(output.communicate()[0])


if __name__ == '__main__':
    delete_images(no_tag(get_docker_images()))