From 45892f99bc723a269d476c8b41059586ca169351 Mon Sep 17 00:00:00 2001 From: Tom Stockton Date: Thu, 14 Jul 2016 11:16:00 +0100 Subject: [PATCH] Add flag to not garbage collect any containers. --- README.md | 7 +++++++ docker-gc | 13 ++++++++----- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index ed050d0..9a3bbcc 100644 --- a/README.md +++ b/README.md @@ -96,6 +96,13 @@ mariadb-data drunk_goodall ``` +If you would prefer to not garbage collect any containers at all then +you can do this by setting the environment variable below: + +``` +REMOVE_CONTAINERS=0 docker-gc +``` + ### Forcing deletion of images that have multiple tags By default, docker will not remove an image if it is tagged in multiple diff --git a/docker-gc b/docker-gc index 735d4f0..284a7b0 100755 --- a/docker-gc +++ b/docker-gc @@ -44,6 +44,7 @@ set -o errexit GRACE_PERIOD_SECONDS=${GRACE_PERIOD_SECONDS:=3600} STATE_DIR=${STATE_DIR:=/var/lib/docker-gc} +REMOVE_CONTAINERS=${REMOVE_CONTAINERS:=1} FORCE_CONTAINER_REMOVAL=${FORCE_CONTAINER_REMOVAL:=0} FORCE_IMAGE_REMOVAL=${FORCE_IMAGE_REMOVAL:=0} DOCKER=${DOCKER:=docker} @@ -252,11 +253,13 @@ if [[ $FORCE_CONTAINER_REMOVAL -gt 0 ]]; then FORCE_CONTAINER_FLAG="-f" fi # Reap containers. -if [[ $DRY_RUN -gt 0 ]]; then - container_log "The following container would have been removed" containers.reap -else - container_log "Removing containers" containers.reap - xargs -n 1 $DOCKER rm $FORCE_CONTAINER_FLAG --volumes=true < containers.reap &>/dev/null || true +if [[ $REMOVE_CONTAINERS -gt 0 ]]; then + if [[ $DRY_RUN -gt 0 ]]; then + container_log "The following container would have been removed" containers.reap + else + container_log "Removing containers" containers.reap + xargs -n 1 $DOCKER rm $FORCE_CONTAINER_FLAG --volumes=true < containers.reap &>/dev/null || true + fi fi # Use -f flag on docker rmi command; forces removal of images that have multiple tags