Skip to content

Commit 6488384

Browse files
author
Dylan Grafmyre
committed
Keep tag by regex
Adds `--keep-regex` paramiter which can take multiple python stdlib re expressions and keep matching tags from entering the deletion set. Example paramiter: --keep-regex '.*:latest' 'ubuntu.*' This example will ingore all tags starting with "ubuntu" and all images ending in ":latest".
1 parent 48c40e8 commit 6488384

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

dockerrotate/main.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"""
44
from argparse import ArgumentParser
55
from datetime import timedelta
6+
import re
67

78
from dateutil import parser
89
from docker import Client
@@ -40,6 +41,13 @@ def parse_args():
4041
default=3,
4142
help="Keep this many images of each kind",
4243
)
44+
parser.add_argument(
45+
"--keep-regex",
46+
type=str,
47+
nargs='*',
48+
default=list(),
49+
help="Python regex of tag names to keep.",
50+
)
4351
parser.add_argument(
4452
"--only",
4553
"-o",
@@ -167,6 +175,9 @@ def clean_images(client, args):
167175
image_name = normalize_tag_name(tag)
168176
if args.only and args.only != image_name:
169177
continue
178+
if any([re.match(pattern, tag) for pattern in args.keep_regex]):
179+
continue
180+
170181
images_by_name.setdefault(image_name, set()).add(image["Id"])
171182

172183
for image_name, image_ids in images_by_name.items():

0 commit comments

Comments
 (0)