Skip to content
master
Go to file
Code

Latest commit

Signed-off-by: Thomas Sjögren <konstruktoid@users.noreply.github.com>
c17fe49

Git stats

Files

Permalink
Failed to load latest commit information.

README.adoc

docker-garby

Dinotrux character Garby

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

Docker version post-2017

You should be using a Docker version released later than 2017 with the docker prune command available. If you don’t need to exclude containers or images, use that instead.

Running docker-garby

You can either run docker-garby as a shell script on systems using GNU coreutils or in a 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 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.

dockerPrune=${dockerPrune:=no} # (1)
pruneOptions=${pruneOptions} # (2)
excludeImages=${excludeImages:="$(pwd)/docker-garby.exclude"} # (3)
logFile='syslog' # (4)
maxSecondsOld=${maxSecondsOld:=3600} # (5)
networkPrune=${networkPrune:=yes} # (6)
pullExcluded=${pullExcluded:=yes} # (7)
  1. If you want to use the internal prune option, available in Docker 1.13. Negates maxSecondsOld and excludeImages.

  2. Configure docker {container|system|network} prune options, if Docker 17.04 or later.

  3. Exclude the images listed in this file.

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

  5. How old a container has to be in seconds before removing it.

  6. Remove unused networks using the internal prune option, available in Docker 1.13.

  7. Pull the latest version of the excluded images.

Excluding images

The file available to excludeImages above, should contain either the name and version, a label (starting with label:) or the SHA256 digest (starting with sha256:).

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

Ansible

An docker-garby Ansible playbook is available at konstruktoid/Ansible.

Examples

Shell

$ dockerPrune=yes pruneOptions="--filter until=1m" sh docker-garby.sh
Deleted Containers:
8b8dfbad1a37b4fabcfa892974f5a255a6aaa8cef921cc323d637f35919d61e1

Total reclaimed space: 0B
Total reclaimed space: 0B

$ sudo journalctl SYSLOG_IDENTIFIER=docker-garby
...
Feb 10 14:21:49 lab01 docker-garby[3484]: clientVersion: 18061
Feb 10 14:21:49 lab01 docker-garby[3492]: dockerPrune: yes
Feb 10 14:21:49 lab01 docker-garby[3500]: pruneOptions: --filter until=1m
Feb 10 14:21:49 lab01 docker-garby[3508]: excludeImages: /home/vagrant/vagrant/docker-garby.exclude
Feb 10 14:21:49 lab01 docker-garby[3516]: logFile: syslog
Feb 10 14:21:49 lab01 docker-garby[3524]: maxSecondsOld: 3600
Feb 10 14:21:49 lab01 docker-garby[3532]: pullExcluded: yes
Feb 10 14:21:49 lab01 docker-garby[3540]: serverVersion: 18061
Feb 10 14:21:49 lab01 docker-garby[3573]: Using docker container prune.
Feb 10 14:21:49 lab01 docker-garby[3602]: Using docker system prune.
Feb 10 14:21:49 lab01 docker-garby[3627]: No dangling volumes found.
Feb 10 14:21:49 lab01 docker-garby[3635]: Using docker network prune.
$ 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 build --no-cache --tag konstruktoid/docker-garby -f Dockerfile .
$ 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.

Contributing

Do you want to contribute? That’s great! Contributions are always welcome, no matter how large or small. If you found something odd, feel free to submit a new issue, improve the code by creating a pull request, or by sponsoring this project.

About

Just another Docker maintenance script, managing garbage collection of Docker containers and images.

Topics

Resources

License

Sponsor this project

 
Learn more about GitHub Sponsors
You can’t perform that action at this time.