summaryrefslogtreecommitdiff
path: root/global/overlay/usr/local/bin/docker-cleanup
diff options
context:
space:
mode:
authorLeif Johansson <leifj@sunet.se>2015-03-22 01:12:40 +0100
committerLeif Johansson <leifj@sunet.se>2015-03-22 01:12:40 +0100
commit3a8ee7ace2bdd268d870fed9755fb91f21a10c5b (patch)
treea633960bce278f587ed5b4a29d54ffc529590d50 /global/overlay/usr/local/bin/docker-cleanup
parent73bc205b46926c0b775e484b4f7d8c14611f0b8f (diff)
parentc04894515146e2e762d25abcff275c0d03dfe0c6 (diff)
Merge branch 'multiverse'
Conflicts: global/overlay/etc/puppet/cosmos-modules.conf
Diffstat (limited to 'global/overlay/usr/local/bin/docker-cleanup')
-rwxr-xr-xglobal/overlay/usr/local/bin/docker-cleanup46
1 files changed, 46 insertions, 0 deletions
diff --git a/global/overlay/usr/local/bin/docker-cleanup b/global/overlay/usr/local/bin/docker-cleanup
new file mode 100755
index 0000000..f46942b
--- /dev/null
+++ b/global/overlay/usr/local/bin/docker-cleanup
@@ -0,0 +1,46 @@
+#!/bin/sh
+# Cleanup docker files: untagged containers and images.
+#
+# Use `docker-cleanup -n` for a dry run to see what would be deleted.
+
+untagged_containers() {
+ # Print containers using untagged images: $1 is used with awk's print: 0=line, 1=column 1.
+ # NOTE: "[0-9a-f]{12}" does not work with GNU Awk 3.1.7 (RHEL6).
+ # Ref: https://github.com/blueyed/dotfiles/commit/a14f0b4b#commitcomment-6736470
+ docker ps -a | tail -n +2 | awk '$2 ~ "^[0-9a-f]+$" {print $'$1'}'
+}
+
+untagged_images() {
+ # Print untagged images: $1 is used with awk's print: 0=line, 3=column 3.
+ # NOTE: intermediate images (via -a) seem to only cause
+ # "Error: Conflict, foobarid wasn't deleted" messages.
+ # Might be useful sometimes when Docker messed things up?!
+ # docker images -a | awk '$1 == "<none>" {print $'$1'}'
+ docker images | tail -n +2 | awk '$1 == "<none>" {print $'$1'}'
+}
+
+# Dry-run.
+if [ "$1" = "-n" ]; then
+ echo "=== Containers with uncommitted images: ==="
+ untagged_containers 0
+ echo
+
+ echo "=== Uncommitted images: ==="
+ untagged_images 0
+
+ exit
+fi
+if [ -n "$1" ]; then
+ echo "Cleanup docker files: remove untagged containers and images."
+ echo "Usage: ${0##*/} [-n]"
+ echo " -n: dry run: display what would get removed."
+ exit 1
+fi
+
+# Remove containers with untagged images.
+echo "Removing containers:" >&2
+untagged_containers 1 | xargs --no-run-if-empty docker rm --volumes=true
+
+# Remove untagged images
+echo "Removing images:" >&2
+untagged_images 3 | xargs --no-run-if-empty docker rmi