Skip to content
This repository has been archived by the owner on Jun 11, 2019. It is now read-only.

Commit

Permalink
Added ability to exclude containers from clean up. This includes those
Browse files Browse the repository at this point in the history
in an exited on dead state.
  • Loading branch information
alejom99 committed Feb 16, 2016
1 parent bafa457 commit f230785
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 11 deletions.
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
15 changes: 8 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -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 <none> images to be left around.

## Environment Variables
Expand All @@ -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
Expand All @@ -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.
Expand Down
19 changes: 15 additions & 4 deletions run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit f230785

Please sign in to comment.