From b69980f618344b6430498709a2e2092daacf9f80 Mon Sep 17 00:00:00 2001 From: andrewsykim Date: Mon, 4 Apr 2016 12:10:47 -0400 Subject: [PATCH] store the list of containers/images kept and removed by docker-gc copy all files in one command Update README update all files --- README.md | 17 ++++++++++++----- docker-gc | 14 +++++++++++--- 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 6db22f0..61b99f6 100644 --- a/README.md +++ b/README.md @@ -83,11 +83,11 @@ redis:[^ ]\+ ### Excluding Containers From Garbage Collection -There can also be containers (for example data only containers) which -you would like to exclude from garbage collection. To do so, create -`/etc/docker-gc-exclude-containers`, or if you want the file to be -read from elsewhere, set the `EXCLUDE_CONTAINERS_FROM_GC` environment -variable to its location. This file should container name patterns (in +There can also be containers (for example data only containers) which +you would like to exclude from garbage collection. To do so, create +`/etc/docker-gc-exclude-containers`, or if you want the file to be +read from elsewhere, set the `EXCLUDE_CONTAINERS_FROM_GC` environment +variable to its location. This file should container name patterns (in the `grep` sense), one per line, such as `mariadb-data`. An example container excludes file might contain: @@ -136,6 +136,13 @@ By default, docker-gc will proceed with deletion of containers and images. To te DRY_RUN=1 docker-gc ``` +### Keeping History +You can configure docker-gc to store a history of images and containers that were removed +by setting the `LOG_HISTORY` variable to 1. + +``` +LOG_HISTORY=1 docker-gc +``` ## Running as a Docker Image diff --git a/docker-gc b/docker-gc index f62ce13..f1b0b05 100755 --- a/docker-gc +++ b/docker-gc @@ -37,13 +37,14 @@ # containers were removed. To enable logging to syslog, set LOG_TO_SYSLOG=1. # When disabled, this script will instead log to standard out. When syslog is # enabled, the syslog facility and logger can be configured with -# $SYSLOG_FACILITY and $SYSLOG_LEVEL respectively. +# $SYSLOG_FACILITY and $SYSLOG_LEVEL respectively. set -o nounset set -o errexit GRACE_PERIOD_SECONDS=${GRACE_PERIOD_SECONDS:=3600} STATE_DIR=${STATE_DIR:=/var/lib/docker-gc} +LOG_HISTORY=${LOG_HISTORY:=0} FORCE_CONTAINER_REMOVAL=${FORCE_CONTAINER_REMOVAL:=0} FORCE_IMAGE_REMOVAL=${FORCE_IMAGE_REMOVAL:=0} DOCKER=${DOCKER:=docker} @@ -75,7 +76,7 @@ fi EXCLUDE_CONTAINERS_FROM_GC=${EXCLUDE_CONTAINERS_FROM_GC:=/etc/docker-gc-exclude-containers} if [ ! -f "$EXCLUDE_CONTAINERS_FROM_GC" ] then - EXCLUDE_CONTAINERS_FROM_GC=/dev/null + EXCLUDE_CONTAINERS_FROM_GC=/dev/null fi EXCLUDE_IDS_FILE="exclude_ids" @@ -143,7 +144,7 @@ function compute_exclude_container_ids() { return fi # Find all docker images - # Filter out with matching names + # Filter out with matching names # and put them to $EXCLUDE_CONTAINER_IDS_FILE $DOCKER ps -a \ | grep -E "$PROCESSED_EXCLUDES" \ @@ -272,3 +273,10 @@ else image_log "Removing image" images.reap xargs -n 1 $DOCKER rmi $FORCE_IMAGE_FLAG < images.reap &>/dev/null || true fi + +if [[ $LOG_HISTORY -gt 0 ]]; then + timestamp=$(date +"%H-%M-%S-%Y-%m-%d") + log "Saving docker gc history under $STATE_DIR/history/$timestamp" + mkdir -p $STATE_DIR/history/$timestamp + cp $STATE_DIR/* $STATE_DIR/history/$timestamp +fi