Skip to content
This repository has been archived by the owner on Feb 2, 2021. It is now read-only.

Save docker-gc history of kept/removed containers/images #98

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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

Expand Down
14 changes: 11 additions & 3 deletions docker-gc
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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" \
Expand Down Expand Up @@ -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