diff --git a/README.md b/README.md index 584355b..0961a18 100644 --- a/README.md +++ b/README.md @@ -13,6 +13,7 @@ A simple Docker container and image garbage collection script. * Containers that exited more than an hour ago are removed. * Images that don't belong to any remaining container after that are removed. +* Optionally, remove volumes that are not associated to any remaining container after removal (Available only for docker >= 1.9.0) Although docker normally prevents removal of images that are in use by containers, we take extra care to not remove any image tags (e.g., ubuntu:14.04, @@ -101,6 +102,14 @@ mariadb-data drunk_goodall ``` +### Excluding Volumes From Garbage Collection + +There can be occasions where you don't want to remove a dangling volume. +To enable this functionality you can create a file named +`/etc/docker-gc-exclude-volumes` (or specify `EXCLUDE_VOLUMES_IDS_FILE` env var +with any path for such file), containing name patterns (in the `grep` sense), +one per line, of volumes that will be excluded from garbage collection. + ### Forcing deletion of images that have multiple tags By default, docker will not remove an image if it is tagged in multiple @@ -184,6 +193,14 @@ $ docker run --rm -v /var/run/docker.sock:/var/run/docker.sock -v /etc:/etc:ro s The `/etc` directory is also mapped so that it can read any exclude files that you've created. +If you want to remove volumes, you can do so by passing REMOVE_VOLUMES env var set to 1. + +```sh +$ docker run --rm -v /var/run/docker.sock:/var/run/docker.sock -v /etc:/etc -e REMOVE_VOLUMES=1 spotify/docker-gc +``` + +If you want to remove volumes only for a specified driver, you can do it by passing VOLUME_DELETE_ONLY_DRIVER env var set to the driver name. + If your docker daemon is configured to run with user namespace, you will need to run the container with [user namespace disabled][disable-user-namespace]: @@ -191,4 +208,4 @@ run the container with [user namespace disabled][disable-user-namespace]: $ docker run --rm --userns host -v /var/run/docker.sock:/var/run/docker.sock -v /etc:/etc spotify/docker-gc ``` -[disable-user-namespace]: https://docs.docker.com/engine/reference/commandline/dockerd/#disable-user-namespace-for-a-container +[disable-user-namespace]: https://docs.docker.com/engine/reference/commandline/dockerd/#disable-user-namespace-for-a-container \ No newline at end of file diff --git a/docker-gc b/docker-gc index cd7a511..25b2110 100755 --- a/docker-gc +++ b/docker-gc @@ -45,6 +45,7 @@ set -o errexit GRACE_PERIOD_SECONDS=${GRACE_PERIOD_SECONDS:=3600} MINIMUM_IMAGES_TO_SAVE=${MINIMUM_IMAGES_TO_SAVE:=0} STATE_DIR=${STATE_DIR:=/var/lib/docker-gc} +REMOVE_ASSOCIATED_VOLUME=${REMOVE_ASSOCIATED_VOLUME=1} FORCE_CONTAINER_REMOVAL=${FORCE_CONTAINER_REMOVAL:=0} FORCE_IMAGE_REMOVAL=${FORCE_IMAGE_REMOVAL:=0} DOCKER=${DOCKER:=docker} @@ -55,6 +56,9 @@ SYSLOG_LEVEL=${SYSLOG_LEVEL:=info} SYSLOG_TAG=${SYSLOG_TAG:=docker-gc} DRY_RUN=${DRY_RUN:=0} EXCLUDE_DEAD=${EXCLUDE_DEAD:=0} +REMOVE_VOLUMES=${REMOVE_VOLUMES:=0} +EXCLUDE_VOLUMES_IDS_FILE=${EXCLUDE_VOLUMES_IDS_FILE:=/etc/docker-gc-exclude-volumes} +VOLUME_DELETE_ONLY_DRIVER=${VOLUME_DELETE_ONLY_DRIVER:=local} PIDFILE=$PID_DIR/dockergc exec 3>>$PIDFILE @@ -182,6 +186,16 @@ function image_log() { done < "$filename" } +function volumes_log() { + prefix=$1 + filename=$2 + + while IFS='' read -r volumeid + do + log "$prefix $volumeid" + done < "$filename" +} + # Change into the state directory (and create it if it doesn't exist) if [ ! -d "$STATE_DIR" ] then @@ -275,6 +289,15 @@ FORCE_CONTAINER_FLAG="" if [[ $FORCE_CONTAINER_REMOVAL -gt 0 ]]; then FORCE_CONTAINER_FLAG="-f" fi + +# Remove associated volume, so that we won't create new orphan volumes. +if [[ $REMOVE_ASSOCIATED_VOLUME -gt 0 ]]; then + if [[ -z $FORCE_CONTAINER_FLAG ]]; then + FORCE_CONTAINER_FLAG="-v" + else + FORCE_CONTAINER_FLAG=$FORCE_CONTAINER_FLAG"v" + fi +fi # Reap containers. if [[ $DRY_RUN -gt 0 ]]; then container_log "The following container would have been removed" containers.reap @@ -297,3 +320,40 @@ else xargs -n 1 $DOCKER rmi $FORCE_IMAGE_FLAG < images.reap &>/dev/null || true fi +if [[ $REMOVE_VOLUMES -gt 0 ]]; then + set +e + $DOCKER volume ls --filter "dangling=true" -q &> /dev/null + VOLUMES=$? + set -e + # If docker volume ls fails, then is probably not supported by either client or server + if [ ! -f "$EXCLUDE_VOLUMES_IDS_FILE" ] + then + EXCLUDE_VOLUMES_IDS_FILE=/dev/null + fi + + if [[ $VOLUMES -gt 0 ]]; then + set +e + VERSION=$($DOCKER version --format="Client: {{.Client.Version}} Server: {{.Server.Version}}" 2&>/dev/null) + FORMAT=$? + set -e + if [[ $FORMAT -gt 0 ]]; then + log "Removing volumes is not supported for Docker < 1.9.0" + else + MESSAGE="Removing volumes is not supported for for docker version "$($DOCKER version --format="Client: {{.Client.Version}} Server: {{.Server.Version}}") & + log "$MESSAGE" + fi + else + if [[ -z "$VOLUME_DELETE_ONLY_DRIVER" ]]; then + $DOCKER volume ls --filter "dangling=true" -q | grep -v -f $EXCLUDE_VOLUMES_IDS_FILE > volumes.reap + else + $DOCKER volume ls --filter "dangling=true" --filter "driver=$VOLUME_DELETE_ONLY_DRIVER" -q | grep -v -f $EXCLUDE_VOLUMES_IDS_FILE > volumes.reap + fi + + if [[ $DRY_RUN -gt 0 ]]; then + volumes_log "The following volume would have been removed" volumes.reap + else + volumes_log "Removing volume" volumes.reap + xargs -n 1 $DOCKER volume rm < volumes.reap &>/dev/null || true + fi + fi +fi diff --git a/version.txt b/version.txt index 17e51c3..0c62199 100644 --- a/version.txt +++ b/version.txt @@ -1 +1 @@ -0.1.1 +0.2.1