-
Notifications
You must be signed in to change notification settings - Fork 0
/
dockerclean.py
executable file
·93 lines (67 loc) · 2.46 KB
/
dockerclean.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/usr/bin/env python3
"""Utility to clean out unused stuff from docker.
"""
import argparse
import logging
import subprocess
# MARK: Local Utilities
def _run(command: str, directory: str = None):
logging.debug("running: %s", command)
with subprocess.Popen(f"{command}", shell=True, cwd=directory, stdout=subprocess.PIPE) as cmd:
for line in cmd.stdout:
print(f" {line.decode('utf-8').strip()}")
def _process(command: str):
logging.debug("Processing command: %s", command)
with subprocess.Popen(
f"{command}", shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT
) as pipe:
for line in pipe.stdout:
yield line.rstrip()
pipe.communicate()
if pipe.returncode != 0:
raise subprocess.CalledProcessError(pipe.returncode, command)
# MARK: Application
def _remove_exited_processes():
logging.info("Removing exited processes...")
for line in _process("docker ps --all -q -f status=exited"):
items = line.decode("utf-8").split()
for item in items:
print(f" removing: {item}")
subprocess.check_output(["docker", "rm", item])
def _remove_old_container_versions():
logging.info("Removing old container versions...")
images = {}
for line in _process("docker image ls"):
entry = line.decode("utf-8").split()
if entry[0] == "REPOSITORY":
continue
name = entry[0]
tag = entry[1]
if tag == "latest":
continue
image_id = entry[2]
if name in images:
print(f" removing {name}:{tag} ({image_id})")
try:
subprocess.check_output(["docker", "image", "rm", image_id])
except subprocess.CalledProcessError:
print(f" WARNING: could not remove image {image_id}")
else:
images[name] = image_id
def _prune():
logging.info("Pruning...")
_run("docker system prune --force")
_run("docker volume prune --force")
# MARK: Main Entry Point
def _parse_command_line():
parser = argparse.ArgumentParser()
parser.add_argument("--verbose", action="store_true", help="Show debugging information")
return parser.parse_args()
def _main():
args = _parse_command_line()
logging.basicConfig(level=logging.DEBUG if args.verbose else logging.INFO)
_remove_exited_processes()
_remove_old_container_versions()
_prune()
if __name__ == "__main__":
_main()