From f230785328f87d84a5104ca0f089022a93cac844 Mon Sep 17 00:00:00 2001 From: Alejandro Mesa Date: Tue, 16 Feb 2016 12:15:58 -0800 Subject: [PATCH] Added ability to exclude containers from clean up. This includes those in an exited on dead state. --- Dockerfile | 1 + README.md | 15 ++++++++------- run.sh | 19 +++++++++++++++---- 3 files changed, 24 insertions(+), 11 deletions(-) diff --git a/Dockerfile b/Dockerfile index 8e68c39..5e483c2 100644 --- a/Dockerfile +++ b/Dockerfile @@ -10,6 +10,7 @@ ADD docker-cleanup-volumes.sh /docker-cleanup-volumes.sh ENV CLEAN_PERIOD **None** ENV DELAY_TIME **None** ENV KEEP_IMAGES **None** +ENV KEEP_CONTAINERS **None** ENV LOOP true ENTRYPOINT ["/run.sh"] diff --git a/README.md b/README.md index 009b799..8884486 100644 --- a/README.md +++ b/README.md @@ -1,12 +1,12 @@ # Docker Cleanup -This image will periodically clean up exited containers and remove images and volumes that aren't in use by a -running container. Based on [tutumcloud/image-cleanup](https://github.com/tutumcloud/image-cleanup) and +This image will periodically clean up exited containers and remove images and volumes that aren't in use by a +running container. Based on [tutumcloud/image-cleanup](https://github.com/tutumcloud/image-cleanup) and [chadoe/docker-cleanup-volumes](https://github.com/chadoe/docker-cleanup-volumes) with some small fixes. -Normally any Docker containers that exit are still kept on disk until *docker rm -v* is used to clean -them up. Similarly any images that aren't used any more are kept around. For a cluster node that see -lots of containers start and stop, large amounts of exited containers and old image versions can fill -up the disk. A Jenkins build slave has the same issues, but can also suffer from SNAPSHOT images being +Normally any Docker containers that exit are still kept on disk until *docker rm -v* is used to clean +them up. Similarly any images that aren't used any more are kept around. For a cluster node that see +lots of containers start and stop, large amounts of exited containers and old image versions can fill +up the disk. A Jenkins build slave has the same issues, but can also suffer from SNAPSHOT images being continuously rebuilt and causing untagged images to be left around. ## Environment Variables @@ -15,6 +15,7 @@ The default parameters can be overridden by setting environment variables on the * **CLEAN_PERIOD=1800** - Interval in seconds to sleep after completing a cleaning run. Defaults to 1800 seconds = 30 minutes. * **DELAY_TIME=1800** - Seconds to wait before removing exited containers and unused images. Defaults to 1800 seconds = 30 minutes. * **KEEP_IMAGES** - List of images to avoid cleaning, e.g. "ubuntu:trusty, ubuntu:latest". Defaults to clean all unused images. + * **KEEP_CONTAINERS** - List of images for exited or dead containers to avoid cleaning, e.g. "ubuntu:trusty, ubuntu:latest". * **LOOP** - Add the ability to do non-looped cleanups, run it once and exit. Options are true, false. Defaults to true to run it forever in loops. ## Deployment @@ -24,7 +25,7 @@ If the */var/lib/docker* directory is mapped into the container this script will ### Systemd and CoreOS/Fleet -Create a [Systemd unit](http://www.freedesktop.org/software/systemd/man/systemd.unit.html) file +Create a [Systemd unit](http://www.freedesktop.org/software/systemd/man/systemd.unit.html) file in **/etc/systemd/system/docker-cleanup.service** with contents like below. Using CoreOS and [Fleet](https://coreos.com/docs/launching-containers/launching/fleet-unit-files/) then add the X-Fleet section to schedule the unit on all cluster nodes. diff --git a/run.sh b/run.sh index cea5f86..6d38a19 100755 --- a/run.sh +++ b/run.sh @@ -28,6 +28,13 @@ if [ "${KEEP_IMAGES}" == "**None**" ]; then unset KEEP_IMAGES fi +arr_keep_containers="" +if [ "${KEEP_CONTAINERS}" != "**None**" ]; then + arr_keep_containers=$(echo ${KEEP_CONTAINERS} | tr "," "\n") +fi +unset KEEP_CONTAINERS + + if [ "${LOOP}" != "false" ]; then LOOP=true fi @@ -43,10 +50,14 @@ do # Cleanup exited/dead containers EXITED_CONTAINERS_IDS="`docker ps -a -q -f status=exited -f status=dead | xargs echo`" - if [ "$EXITED_CONTAINERS_IDS" != "" ]; then - echo "Removing exited containers" - docker rm -v $EXITED_CONTAINERS_IDS - fi + for CONTAINER_ID in $EXITED_CONTAINERS_IDS; do + CONTAINER_IMAGE=$(docker inspect --format='{{(index .Config.Image)}}' $CONTAINER_ID) + if [[ ! "${arr_keep_containers[@]}" =~ "${CONTAINER_IMAGE}" ]]; then + echo "Removing container $CONTAINER_ID" + docker rm -v $CONTAINER_ID + fi + done + unset CONTAINER_ID # Get all containers in "created" state rm -f CreatedContainerIdList