patternpythonMinor
Removing untagged docker images using Python
Viewed 0 times
removingdockeruntaggedusingpythonimages
Problem
I have written a piece of code in Python for removing the docker images which are not tagged only:
Python code:
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
# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
release 3.1 b6bf9d19cc6c 5 hours ago 869MB
release 3.2 b6bf9d19cc6c 5 hours ago 869MB
3dfdfcb0769d 6 hours ago 433MB
d505190470fd 6 hours ago 433MB
979a42368814 7 hours ago 433MB
f8bcf895ffce 7 hours ago 433MB
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.4MBPython 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 ("" == 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
Solution
I would split the line of the output once and re-use that. Also, if you don't pass a separator to
I would probably change
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:
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] == "":
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] == "" to if line[1] == "" 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] == "":
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()))Code Snippets
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] == "<none>":
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])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] == "<none>":
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()))Context
StackExchange Code Review Q#161389, answer score: 2
Revisions (0)
No revisions yet.