Skip to content
This repository was archived by the owner on Jun 11, 2019. It is now read-only.

Commit 3edda8a

Browse files
committed
Merge pull request #22 from DaveDeCaprio/master
Added support to keep containers by their container name
2 parents 46aae2b + b41be70 commit 3edda8a

File tree

2 files changed

+30
-12
lines changed

2 files changed

+30
-12
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,11 @@ The default parameters can be overridden by setting environment variables on the
2121
* **DELAY_TIME=1800** - Seconds to wait before removing exited containers and unused images. Defaults to 1800 seconds = 30 minutes.
2222
* **KEEP_IMAGES** - List of images to avoid cleaning, e.g. "ubuntu:trusty, ubuntu:latest". Defaults to clean all unused images.
2323
* **KEEP_CONTAINERS** - List of images for exited or dead containers to avoid cleaning, e.g. "ubuntu:trusty, ubuntu:latest".
24+
* **KEEP_CONTAINERS_NAMED** - List of names for exited or dead containers to avoid cleaning, e.g. "my-container1, persistent-data".
2425
* **LOOP** - Add the ability to do non-looped cleanups, run it once and exit. Options are true, false. Defaults to true to run it forever in loops.
2526
* **DEBUG** - Set to 1 to enable more debugging output on pattern matches
2627

27-
Note that **KEEP_IMAGES** and **KEEP_CONTAINERS** are left-anchored bash shell pattern matching lists (NOT regexps). Therefore, the image **foo/bar:tag** will be matched by ANY of the following:
28+
Note that **KEEP_IMAGES**, **KEEP_CONTAINERS**, and **KEEP_CONTAINERS_NAMED** are left-anchored bash shell pattern matching lists (NOT regexps). Therefore, the image **foo/bar:tag** will be matched by ANY of the following:
2829

2930
* foo/bar:tag
3031
* foo/bar

run.sh

100755100644
+28-11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
11
#!/bin/bash
22

3+
checkPatterns() {
4+
keepit=$3
5+
if [ -n "$1" ]; then
6+
for PATTERN in $(echo $1 | tr "," "\n"); do
7+
if [[ "$2" = $PATTERN* ]]; then
8+
if [ $DEBUG ]; then echo "DEBUG: Matches $PATTERN - keeping"; fi
9+
keepit=1
10+
else
11+
if [ $DEBUG ]; then echo "DEBUG: No match for $PATTERN"; fi
12+
fi
13+
done
14+
fi
15+
return $keepit
16+
}
17+
318
if [ ! -e "/var/run/docker.sock" ]; then
419
echo "=> Cannot find docker socket(/var/run/docker.sock), please check the command!"
520
exit 1
@@ -35,6 +50,13 @@ if [ "${KEEP_CONTAINERS}" == "**All**" ]; then
3550
KEEP_CONTAINERS="."
3651
fi
3752

53+
if [ "${KEEP_CONTAINERS_NAMED}" == "**None**" ]; then
54+
unset KEEP_CONTAINERS_NAMED
55+
fi
56+
if [ "${KEEP_CONTAINERS_NAMED}" == "**All**" ]; then
57+
KEEP_CONTAINERS_NAMED="."
58+
fi
59+
3860
if [ "${LOOP}" != "false" ]; then
3961
LOOP=true
4062
fi
@@ -65,18 +87,13 @@ do
6587
EXITED_CONTAINERS_IDS="`docker ps -a -q -f status=exited -f status=dead | xargs echo`"
6688
for CONTAINER_ID in $EXITED_CONTAINERS_IDS; do
6789
CONTAINER_IMAGE=$(docker inspect --format='{{(index .Config.Image)}}' $CONTAINER_ID)
68-
if [ $DEBUG ]; then echo "DEBUG: Check container $CONTAINER_IMAGE"; fi
90+
CONTAINER_NAME=$(docker inspect --format='{{(index .Name)}}' $CONTAINER_ID)
91+
if [ $DEBUG ]; then echo "DEBUG: Check container image $CONTAINER_IMAGE named $CONTAINER_NAME"; fi
6992
keepit=0
70-
if [ -n "${KEEP_CONTAINERS}" ]; then
71-
for PATTERN in $(echo ${KEEP_CONTAINERS} | tr "," "\n"); do
72-
if [[ "${CONTAINER_IMAGE}" = $PATTERN* ]]; then
73-
if [ $DEBUG ]; then echo "DEBUG: Matches $PATTERN - keeping"; fi
74-
keepit=1
75-
else
76-
if [ $DEBUG ]; then echo "DEBUG: No match for $PATTERN"; fi
77-
fi
78-
done
79-
fi
93+
checkPatterns "${KEEP_CONTAINERS}" "${CONTAINER_IMAGE}" $keepit
94+
keepit=$?
95+
checkPatterns "${KEEP_CONTAINERS_NAMED}" "${CONTAINER_NAME}" $keepit
96+
keepit=$?
8097
if [[ $keepit -eq 0 ]]; then
8198
echo "Removing stopped container $CONTAINER_ID"
8299
docker rm -v $CONTAINER_ID

0 commit comments

Comments
 (0)