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

Commit

Permalink
Merge pull request #22 from DaveDeCaprio/master
Browse files Browse the repository at this point in the history
Added support to keep containers by their container name
  • Loading branch information
mikljohansson committed Mar 31, 2016
2 parents 46aae2b + b41be70 commit 3edda8a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 12 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ The default parameters can be overridden by setting environment variables on the
* **DELAY_TIME=1800** - Seconds to wait before removing exited containers and unused images. Defaults to 1800 seconds = 30 minutes.
* **KEEP_IMAGES** - List of images to avoid cleaning, e.g. "ubuntu:trusty, ubuntu:latest". Defaults to clean all unused images.
* **KEEP_CONTAINERS** - List of images for exited or dead containers to avoid cleaning, e.g. "ubuntu:trusty, ubuntu:latest".
* **KEEP_CONTAINERS_NAMED** - List of names for exited or dead containers to avoid cleaning, e.g. "my-container1, persistent-data".
* **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.
* **DEBUG** - Set to 1 to enable more debugging output on pattern matches

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:
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:

* foo/bar:tag
* foo/bar
Expand Down
39 changes: 28 additions & 11 deletions run.sh
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
#!/bin/bash

checkPatterns() {
keepit=$3
if [ -n "$1" ]; then
for PATTERN in $(echo $1 | tr "," "\n"); do
if [[ "$2" = $PATTERN* ]]; then
if [ $DEBUG ]; then echo "DEBUG: Matches $PATTERN - keeping"; fi
keepit=1
else
if [ $DEBUG ]; then echo "DEBUG: No match for $PATTERN"; fi
fi
done
fi
return $keepit
}

if [ ! -e "/var/run/docker.sock" ]; then
echo "=> Cannot find docker socket(/var/run/docker.sock), please check the command!"
exit 1
Expand Down Expand Up @@ -35,6 +50,13 @@ if [ "${KEEP_CONTAINERS}" == "**All**" ]; then
KEEP_CONTAINERS="."
fi

if [ "${KEEP_CONTAINERS_NAMED}" == "**None**" ]; then
unset KEEP_CONTAINERS_NAMED
fi
if [ "${KEEP_CONTAINERS_NAMED}" == "**All**" ]; then
KEEP_CONTAINERS_NAMED="."
fi

if [ "${LOOP}" != "false" ]; then
LOOP=true
fi
Expand Down Expand Up @@ -65,18 +87,13 @@ do
EXITED_CONTAINERS_IDS="`docker ps -a -q -f status=exited -f status=dead | xargs echo`"
for CONTAINER_ID in $EXITED_CONTAINERS_IDS; do
CONTAINER_IMAGE=$(docker inspect --format='{{(index .Config.Image)}}' $CONTAINER_ID)
if [ $DEBUG ]; then echo "DEBUG: Check container $CONTAINER_IMAGE"; fi
CONTAINER_NAME=$(docker inspect --format='{{(index .Name)}}' $CONTAINER_ID)
if [ $DEBUG ]; then echo "DEBUG: Check container image $CONTAINER_IMAGE named $CONTAINER_NAME"; fi
keepit=0
if [ -n "${KEEP_CONTAINERS}" ]; then
for PATTERN in $(echo ${KEEP_CONTAINERS} | tr "," "\n"); do
if [[ "${CONTAINER_IMAGE}" = $PATTERN* ]]; then
if [ $DEBUG ]; then echo "DEBUG: Matches $PATTERN - keeping"; fi
keepit=1
else
if [ $DEBUG ]; then echo "DEBUG: No match for $PATTERN"; fi
fi
done
fi
checkPatterns "${KEEP_CONTAINERS}" "${CONTAINER_IMAGE}" $keepit
keepit=$?
checkPatterns "${KEEP_CONTAINERS_NAMED}" "${CONTAINER_NAME}" $keepit
keepit=$?
if [[ $keepit -eq 0 ]]; then
echo "Removing stopped container $CONTAINER_ID"
docker rm -v $CONTAINER_ID
Expand Down

0 comments on commit 3edda8a

Please sign in to comment.