From 4900a312e563eb71a432e63a6881185d7f0d5946 Mon Sep 17 00:00:00 2001 From: Ning Bo Date: Thu, 31 Aug 2017 09:43:57 +0800 Subject: [PATCH 1/3] Fix 'EXCLUDE_CONTAINER_IDS_FILE' will not be cleanup if 'EXCLUDE_CONTAINERS_FROM_GC' profile become empty. --- docker-gc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docker-gc b/docker-gc index 28a0183..c406432 100755 --- a/docker-gc +++ b/docker-gc @@ -140,7 +140,7 @@ function compute_exclude_container_ids() { | sed -e 's/ /\|/g'` # The empty string would match everything if [ "$PROCESSED_EXCLUDES" = "" ]; then - touch $EXCLUDE_CONTAINER_IDS_FILE + > $EXCLUDE_CONTAINER_IDS_FILE return fi # Find all docker images From abf35d763fd2126fad3994e40b5ca9aafce53055 Mon Sep 17 00:00:00 2001 From: Ning Bo Date: Wed, 6 Sep 2017 11:32:37 +0800 Subject: [PATCH 2/3] Add 'EXCLUDE_CREATED' and 'EXCLUDE_ALL_IMAGES'. EXCLUDE_CREATED=1 means don't clean containers that has been created but never run. EXCLUDE_ALL_IMAGES=1 means only clean containers. --- docker-gc | 37 ++++++++++++++++++++++++++----------- 1 file changed, 26 insertions(+), 11 deletions(-) diff --git a/docker-gc b/docker-gc index c406432..265dd65 100755 --- a/docker-gc +++ b/docker-gc @@ -55,6 +55,8 @@ SYSLOG_LEVEL=${SYSLOG_LEVEL:=info} SYSLOG_TAG=${SYSLOG_TAG:=docker-gc} DRY_RUN=${DRY_RUN:=0} EXCLUDE_DEAD=${EXCLUDE_DEAD:=0} +EXCLUDE_CREATED=${EXCLUDE_CREATED:=0} +EXCLUDE_ALL_IMAGES=${EXCLUDE_ALL_IMAGES:=0} PIDFILE=$PID_DIR/dockergc exec 3>>$PIDFILE @@ -211,11 +213,19 @@ comm -23 containers.all containers.running > containers.exited if [[ $EXCLUDE_DEAD -gt 0 ]]; then echo "Excluding dead containers" # List dead containers - $DOCKER ps -q -a -f status=dead | sort | uniq > containers.dead + $DOCKER ps -q --no-trunc -a -f status=dead | sort | uniq > containers.dead comm -23 containers.exited containers.dead > containers.exited.tmp cat containers.exited.tmp > containers.exited fi +if [[ $EXCLUDE_CREATED -gt 0 ]]; then + echo "Excluding created but not running containers" + # List create but not running containers + $DOCKER ps -q --no-trunc -a -f status=created | sort | uniq > containers.created + comm -23 containers.exited containers.created > containers.exited.tmp + cat containers.exited.tmp > containers.exited +fi + container_log "Container not running" containers.exited # Find exited containers that finished at least GRACE_PERIOD_SECONDS ago @@ -255,16 +265,21 @@ do done # Find images that are created at least GRACE_PERIOD_SECONDS ago -> images.reap.tmp -cat images.all | sort | uniq | while read line -do - CREATED=$(${DOCKER} inspect -f "{{.Created}}" ${line}) - ELAPSED=$(elapsed_time $CREATED) - if [[ $ELAPSED -gt $GRACE_PERIOD_SECONDS ]]; then - echo $line >> images.reap.tmp - fi -done -comm -23 images.reap.tmp images.used | grep -E -v -f $EXCLUDE_IDS_FILE > images.reap || true +if [[ $EXCLUDE_ALL_IMAGES -gt 0 ]]; then + echo "Exclude all images" + > images.reap +else + > images.reap.tmp + cat images.all | sort | uniq | while read line + do + CREATED=$(${DOCKER} inspect -f "{{.Created}}" ${line}) + ELAPSED=$(elapsed_time $CREATED) + if [[ $ELAPSED -gt $GRACE_PERIOD_SECONDS ]]; then + echo $line >> images.reap.tmp + fi + done + comm -23 images.reap.tmp images.used | grep -E -v -f $EXCLUDE_IDS_FILE > images.reap || true +fi # Use -f flag on docker rm command; forces removal of images that are in Dead # status or give errors when removing. From aa0f0f0df07a880e7bd3afc816ff16fc76e5b7df Mon Sep 17 00:00:00 2001 From: Ning Bo Date: Wed, 6 Sep 2017 15:38:57 +0800 Subject: [PATCH 3/3] Add 'MINIMUM_CONTAINERS_TO_SAVE' 'MINIMUM_CONTAINERS_TO_SAVE=N' means exclude lastest N containers each images. --- docker-gc | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/docker-gc b/docker-gc index 265dd65..4fb7c21 100755 --- a/docker-gc +++ b/docker-gc @@ -43,6 +43,7 @@ set -o nounset set -o errexit GRACE_PERIOD_SECONDS=${GRACE_PERIOD_SECONDS:=3600} +MINIMUM_CONTAINERS_TO_SAVE=${MINIMUM_CONTAINERS_TO_SAVE:=0} MINIMUM_IMAGES_TO_SAVE=${MINIMUM_IMAGES_TO_SAVE:=0} STATE_DIR=${STATE_DIR:=/var/lib/docker-gc} FORCE_CONTAINER_REMOVAL=${FORCE_CONTAINER_REMOVAL:=0} @@ -245,6 +246,21 @@ cat containers.reap.tmp | sort | uniq | grep -v -f $EXCLUDE_CONTAINER_IDS_FILE > # List containers that we will keep. comm -23 containers.all containers.reap > containers.keep +# List containers that we will save. +if [[ $MINIMUM_CONTAINERS_TO_SAVE -gt 0 ]]; then + > containers.save.tmp + $DOCKER images --no-trunc --format "{{.ID}}" | uniq | while read line + do + $DOCKER ps -a --no-trunc --format '{{.ID}} {{.CreatedAt}}' -f ancestor=$line \ + | sort -k 2 \ + | tail -n $MINIMUM_CONTAINERS_TO_SAVE \ + | cut -f 1 -d " " >> containers.save.tmp + done + cat containers.save.tmp | sort > containers.save + comm -23 containers.reap containers.save > containers.reap.tmp0 + mv containers.reap.tmp0 containers.reap +fi + # List images used by containers that we keep. cat containers.keep | xargs -n 1 $DOCKER inspect -f '{{.Image}}' 2>/dev/null |