Public | Automated Build

Last pushed: 2 hours ago
Short Description
Just another Docker garbage collection script.
Full Description

= docker-garby

image::http://www.dreamworkstv.com/wp-content/uploads/2015/05/DTX-character-GARBY.jpg[Dinotrux character Garby]

Just another Docker garbage collection script, written for Docker version 1.9
and later.

== Running docker-garby
You can either run docker-garby as a shell script on systems using GNU
coreutils or in a link:Dockerfile[Docker container]. By default it will clean
non-running containers that has been inactive for more than one hour, images,
volumes and, if you're using Docker 1.13 or later, unused networks.

Due to differences between BSD and GNU coreutils it is recommended that you
run docker-garby in a container if you're using macOS/Mac OS X, or
install the coreutils and binutils packages using http://brew.sh/[Homebrew].

== Configuration options
The configuration options may be set in the script itself or at runtime. +
maxSecondsOld=600 pullExcluded=no sh docker-garby.sh will remove a finished
container older than 10 minutes and not pull excluded images. Excluded images
are those you want to keep no matter what.

[source]

dockerPrune=${dockerPrune:=no} # <1>
excludeImages=${excludeImages:="$(pwd)/docker-garby.exclude"} # <2>
logFile='syslog' # <3>
maxSecondsOld=${maxSecondsOld:=3600} # <4>
networkPrune=${networkPrune:=yes} # <5>

pullExcluded=${pullExcluded:=yes} # <6>

<1> If you want to use the internal prune option, available in Docker 1.13.
Negates maxSecondsOld and excludeImages.

<2> Exclude the images listed in this file.

<3> Write to syslog. logFile='./docker-garby.log' will write to a logfile,
and the file name will be appended with -$(date +%Y%m%d).

<4> How old a container has to be in seconds before removing it.

<5> Remove unused networks using the internal prune option, available in Docker 1.13.

<6> Pull the latest version of the excluded images.

=== Excluding images
The file available to excludeImages above, should contain either the name
and version or the sha256 digest.

$ cat docker-garby.exclude
sha256:47cf20d8c26c46fff71be614d9f54997edacfe8d46d51769706e5aba94b16f2b
konstruktoid/nginx:latest

=== Ansible
An docker-garby Ansible playbook is available at https://github.com/konstruktoid/Ansible/blob/master/roles/docker-garby_container/tasks/docker-garby.yml[konstruktoid/Ansible].

== Examples

=== Shell

$ dockerPrune=yes sh docker-garby.sh
Deleted Containers:
a2209b39ffdfbd305e1a940c2cced53aa4217f858508041015dcefcea9dc2a64
550f1d001178d76d0a1fd4f0015aeb41f053fbadc291ec04996473f63778381f
47a31c8b183d826b4608156666223e641f2d32628adb85380e3d4b622d18c22d
8a21af9adc7c8b0432be3749e1073f55085a3161f233e5354098b1de56ffee78
6dd02a7762927470228ab76046e70f7df1060b4bfbcdf74ad129f809cec53e27

Total reclaimed space: 0 B
Deleted Images:
untagged: konstruktoid/privoxy:latest
untagged: konstruktoid/privoxy@sha256:0adaa06fffe2f7cdc80ad53e5ed62337fda54027f6fff5005d348c5f5d16818d
deleted: sha256:f4c8c0d866b641abc09825f2cfbf01d272cf4252ac21bca1009b4f041158f0c1
deleted: sha256:38548bee69829edec5a71dd5f2bd85878683977b2a9ef542061202d307ece4ca
deleted: sha256:0e0484753b293b8362ce8a91aee3cf201a6d9b1b7bac94f1b4f6dba2db14540c
deleted: sha256:a0e90e7c7eefc1ed0501f89ac9c6ac405a166be36214fa1ce0e3803177045f49
deleted: sha256:22b4595ae3d4e328621c70ef8dda5a9277f8404ee9b326a4ec78758b0e258de2

Total reclaimed space: 1.875 MB
$ sh docker-garby.sh
$ sudo journalctl SYSLOG_IDENTIFIER=docker-garby
-- Logs begin at Wed 2016-07-13 01:26:04 CEST, end at Wed 2016-07-20 09:30:45 CEST. --
Jul 20 09:22:22 lab01 docker-garby[34525]: excludeImages: /home/tsj/Git/docker-garby/docker-garby.exclude
Jul 20 09:22:22 lab01 docker-garby[34531]: pullExcluded: yes
Jul 20 09:22:22 lab01 docker-garby[34537]: logFile: syslog
Jul 20 09:22:22 lab01 docker-garby[34543]: maxSecondsOld: 3600
Jul 20 09:22:22 lab01 docker-garby[34620]: Container /nostalgic_newton (08fd5995b8a1) finished 643403 seconds ago.
Jul 20 09:22:22 lab01 docker-garby[34626]: Container /nostalgic_newton (08fd5995b8a1) used image [].
Jul 20 09:22:22 lab01 docker-garby[34635]: Container /nostalgic_newton (08fd5995b8a1) removed.
Jul 20 09:22:23 lab01 docker-garby[34671]: Container /angry_roentgen (f020fe7f8108) finished 643479 seconds ago.
Jul 20 09:22:23 lab01 docker-garby[34677]: Container /angry_roentgen (f020fe7f8108) used image [].
Jul 20 09:22:23 lab01 docker-garby[34686]: Container /angry_roentgen (f020fe7f8108) removed.
Jul 20 09:22:23 lab01 docker-garby[34722]: Container /focused_kare (4c70871cae44) finished 643787 seconds ago.
Jul 20 09:22:23 lab01 docker-garby[34728]: Container /focused_kare (4c70871cae44) used image [].
Jul 20 09:22:23 lab01 docker-garby[34737]: Container /focused_kare (4c70871cae44) removed.
Jul 20 09:22:25 lab01 docker-garby[35074]: Container /hopeful_yalow (92bd220166ac) finished 1762378 seconds ago.
Jul 20 09:22:25 lab01 docker-garby[35080]: Container /hopeful_yalow (92bd220166ac) used image [konstruktoid/ubuntubuild:latest].
Jul 20 09:22:25 lab01 docker-garby[35089]: Container /hopeful_yalow (92bd220166ac) removed.
Jul 20 09:22:25 lab01 docker-garby[35124]: Container /desperate_lichterman (5614bd82697a) finished 1763270 seconds ago.
Jul 20 09:22:25 lab01 docker-garby[35130]: Container /desperate_lichterman (5614bd82697a) used image [yak:latest].
Jul 20 09:22:25 lab01 docker-garby[35139]: Container /desperate_lichterman (5614bd82697a) removed.
Jul 20 09:22:25 lab01 docker-garby[35189]: Container /infallible_ptolemy (e6d0737e4f3e) removed.
Jul 20 09:22:26 lab01 docker-garby[35225]: Container /drunk_euclid (e1dcc6a2dde2) finished 2112106 seconds ago.
Jul 20 09:22:26 lab01 docker-garby[35231]: Container /drunk_euclid (e1dcc6a2dde2) used image [nginx:latest].
Jul 20 09:22:26 lab01 docker-garby[35240]: Container /drunk_euclid (e1dcc6a2dde2) removed.
Jul 20 09:22:26 lab01 docker-garby[35266]: Image [alpine:3.3] (sha256:47cf20d8c26c46fff71be614d9f54997edacfe8d46d51769706e5aba94b16f2b) excluded.
Jul 20 09:22:33 lab01 docker-garby[35279]: Image [alpine:3.3] pulled.
Jul 20 09:22:33 lab01 docker-garby[35292]: Image [konstruktoid/nginx:latest] (sha256:fa15a419f9005114cae823a5ab1b5c123cf729edcbd11e4dff4f4ed3b957595f) excluded.
Jul 20 09:22:35 lab01 docker-garby[35304]: Image [konstruktoid/nginx:latest] pulled.
Jul 20 09:23:04 lab01 docker-garby[35386]: Image [debian:8 debian:latest] (sha256:1b088884749bd93867ddb48ff404d4bbff09a17af8d95bc863efa5d133f87b78) unused.
Jul 20 09:23:18 lab01 docker-garby[35395]: Image [debian:8 debian:latest] (sha256:1b088884749bd93867ddb48ff404d4bbff09a17af8d95bc863efa5d133f87b78) removed.
Jul 20 09:23:18 lab01 docker-garby[35423]: Image [busybox:latest] (sha256:2b8fd9751c4c0f5dd266fcae00707e67a2545ef34f9a29354585f93dac906749) unused.
Jul 20 09:23:19 lab01 docker-garby[35432]: Image [busybox:latest] (sha256:2b8fd9751c4c0f5dd266fcae00707e67a2545ef34f9a29354585f93dac906749) removed.
Jul 20 09:23:19 lab01 docker-garby[35442]: Image [centos:6.6] (sha256:2c886f766286449b76ad90a7ce577b988e48fe5a3ed9a75c53fb81f783946831) unused.
Jul 20 09:23:44 lab01 docker-garby[35452]: Image [centos:6.6] (sha256:2c886f766286449b76ad90a7ce577b988e48fe5a3ed9a75c53fb81f783946831) removed.
Jul 20 09:23:44 lab01 docker-garby[35462]: Image [fedora:22] (sha256:2d3da2084d089bc8405d040807290e0dd9d2b7eba747046e42650eddb6209a39) unused.
Jul 20 09:23:57 lab01 docker-garby[35472]: Image [fedora:22] (sha256:2d3da2084d089bc8405d040807290e0dd9d2b7eba747046e42650eddb6209a39) removed.
Jul 20 09:24:03 lab01 docker-garby[35499]: Image [ubuntu:14.04] (sha256:38c759202e30c0d088367425a0252634c3b401e77d547d16ea25d2af6836224d) unused.
Jul 20 09:24:22 lab01 docker-garby[35508]: Image [ubuntu:14.04] (sha256:38c759202e30c0d088367425a0252634c3b401e77d547d16ea25d2af6836224d) removed.

=== Docker

$ docker run --rm --read-only --tmpfs /tmp:rw,nosuid,nodev -v /var/run/docker.sock:/var/run/docker.sock konstruktoid/docker-garby
[20160706141226] Container /silly_kilby6 (c2aa58249762) finished 3708 seconds ago.
[20160706141226] Container /silly_kilby6 (c2aa58249762) used image [].
[20160706141226] Container /silly_kilby6 (c2aa58249762) removed.
[20160706141226] Container /elegant_lalande (ad385eee7dd8) finished 3737 seconds ago.
[20160706141226] Container /elegant_lalande (ad385eee7dd8) used image [].
[20160706141226] Container /elegant_lalande (ad385eee7dd8) removed.
[20160706141226] Container /kickass_hoover (e053f8b2c3e6) finished 3759 seconds ago.
[20160706141226] Container /kickass_hoover (e053f8b2c3e6) used image [konstruktoid/alpine:latest].
[20160706141226] Container /kickass_hoover (e053f8b2c3e6) removed.
[20160706141226] Image [konstruktoid/alpine:latest] (sha256:d879607f7403c3a12b1d5b0bc8d39e609d41be55b4a49923d33b08e897ad56de) excluded.
[20160706141226] Image [nginx:latest] (sha256:0d409d33b27e47423b049f7f863faa08655a8c901749c2b25b93ca67d01a470d) excluded.
[20160706141226] Image [] (sha256:16786a382fbb2334740de73eb0de534d760b281e0084a78802a076034e09aa72) unused.
[20160706141226] Image [] (sha256:16786a382fbb2334740de73eb0de534d760b281e0084a78802a076034e09aa72) removed.
[20160706141226] Image [alpine:3.3] (sha256:47cf20d8c26c46fff71be614d9f54997edacfe8d46d51769706e5aba94b16f2b) unused.
[20160706141226] ERR: Image [alpine:3.3] (sha256:47cf20d8c26c46fff71be614d9f54997edacfe8d46d51769706e5aba94b16f2b) was not removed.
[20160706141226] Image [] (sha256:83e46309df54bc729f68008d2bec6bcc272e1b5e2f3c6130731c0e8e525faf1c) unused.
[20160706141226] Image [] (sha256:83e46309df54bc729f68008d2bec6bcc272e1b5e2f3c6130731c0e8e525faf1c) removed.
[20160706141226] Image [] (sha256:e908192db440b9d621d89cd87f68bfd4e2e556785089da265d271454eb002c44) unused.
[20160706141226] Image [] (sha256:e908192db440b9d621d89cd87f68bfd4e2e556785089da265d271454eb002c44) removed.
[20160706141226] Image [] (sha256:ea4abdc85a28f70219a0d683c33f2438959f84dae3fe10ef5b964153a13fee33) unused.
[20160706141226] Image [] (sha256:ea4abdc85a28f70219a0d683c33f2438959f84dae3fe10ef5b964153a13fee33) removed.
[20160706141226] No dangling volumes found.
Docker Pull Command
Owner
konstruktoid
Source Repository