From: LuaTenshi Date: Sun, 1 Mar 2015 05:24:45 +0000 (-0500) Subject: Added cleanup command by Supermathie X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=a8b66f980e1c33979970f5930ac146a49c2003c3;p=discourse_docker.git Added cleanup command by Supermathie Added a the cleanup command to the launcher as suggested by Sam on https://meta.discourse.org/t/adding-an-easy-discourse-command/25841/2 The original code was written by Supermathie on https://meta.discourse.org/t/low-on-disk-space-cleaning-up-old-docker-containers/15792/18 --- diff --git a/launcher b/launcher index 450e9ea..1786a75 100755 --- a/launcher +++ b/launcher @@ -50,6 +50,7 @@ usage () { echo " mailtest: Test the mail settings in a container" echo " bootstrap: Bootstrap a container for the config based on a template" echo " rebuild: Rebuild a container (destroy old, bootstrap, start new)" + echo " cleanup: Remove all containers that have stopped for > 24 hours" echo echo "Options:" echo " --skip-prereqs Don't check prerequisites" @@ -298,6 +299,15 @@ RUBY install_docker } +[ $command == "cleanup" ] && { + echo "Starting Cleanup" + docker inspect -f '{{.Id}},{{.State.Running}},{{.State.FinishedAt}}' $(docker ps -qa) | \ + awk -F, 'BEGIN { TIME=strftime("%FT%T.000000000Z",systime()-60*60*24); } $2=="false" && $3 < TIME {print $1;}' | \ + xargs --no-run-if-empty docker rm >/dev/null 2>/dev/null + docker images --no-trunc| grep none | awk '{print $3}' | xargs -r docker rmi + echo "Ending Cleanup" + exit 0 +} [ $# -lt 2 ] && { usage