• This repository has been archived on 11/Jun/2019
  • Stars
    star
    586
  • Rank 75,773 (Top 2 %)
  • Language
    Shell
  • License
    MIT License
  • Created over 9 years ago
  • Updated over 5 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

DEPRECATED Automatic Docker image, container and volume cleanup

DEPRECATED

This repository is no longer activiely maintained.


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 and chadoe/docker-cleanup-volumes with some small fixes.

WARNING: This script will remove all exited containers, data-only containers and unused images unless you carefully exclude them. Take care if you mount /var/lib/docker into the container since that will clean up all unused data volumes. If it's not compatible with your system or Docker version it may delete all your volumes, even from under running containers.

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 images to be left around.

Environment Variables

The default parameters can be overridden by setting environment variables on the container using the docker run -e flag.

  • 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".
  • KEEP_CONTAINERS_NAMED - List of names for exited or dead containers to avoid cleaning, e.g. "my-container1, persistent-data".
  • 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.
  • DEBUG - Set to 1 to enable more debugging output on pattern matches
  • DOCKER_API_VERSION - The docker API version to use. This defaults to 1.20, but you can override it here in case the docker version on your host differs from the one that is installed in this container. You can find this on your host system by running docker version --format '{{.Client.APIVersion}}'.

Note that KEEP_IMAGES, KEEP_CONTAINERS, and KEEP_CONTAINERS_NAMED are left-anchored bash shell pattern matching lists (NOT regexps). Therefore, the image foo/bar:tag will be matched by ANY of the following:

  • foo/bar:tag
  • foo/bar
  • foo/b
  • [[:alpha:]]/bar
  • */*:tag
  • *:tag
  • foo/*:tag

However it will not match

  • foo/baz
  • bar:tag
  • /bar
  • :tag
  • [[:alpha:]]:tag

By default, both are set to **None** which is the same as the blank string. If you want to keep ALL images or containers, effectively disabling this part of the cleanup, then you should use *:* to match all images. Do not use a bare * as this will be taken as a filename match.

Deployment

The image uses the Docker client to to list and remove containers and images. For this reason the Docker client and socket is mapped into the container.

If the /var/lib/docker directory is mapped into the container this script will also clean up orphaned Docker volumes.

Systemd and CoreOS/Fleet

Create a Systemd unit file in /etc/systemd/system/docker-cleanup.service with contents like below. Using CoreOS and Fleet then add the X-Fleet section to schedule the unit on all cluster nodes.

[Unit]
Description=Cleanup of exited containers and unused images/volumes
After=docker.service
Requires=docker.service

[Install]
WantedBy=multi-user.target

[Service]
Environment=IMAGE=meltwater/docker-cleanup:latest NAME=docker-cleanup

# Allow docker pull to take some time
TimeoutStartSec=600

# Restart on failures
KillMode=none
Restart=always
RestartSec=15

ExecStartPre=-/usr/bin/docker kill $NAME
ExecStartPre=-/usr/bin/docker rm $NAME
ExecStartPre=-/bin/sh -c 'if ! docker images | tr -s " " : | grep "^${IMAGE}:"; then docker pull "${IMAGE}"; fi'
ExecStart=/usr/bin/docker run \
    -v /var/run/docker.sock:/var/run/docker.sock:rw \
    -v /var/lib/docker:/var/lib/docker:rw \
    --name=${NAME} \
    $IMAGE

ExecStop=/usr/bin/docker stop $NAME

[X-Fleet]
Global=true

Puppet Hiera

Using the garethr-docker module

classes:
  - docker::run_instance

docker::run_instance:
  'cleanup':
    image: 'meltwater/docker-cleanup:latest'
    volumes:
      - "/var/run/docker.sock:/var/run/docker.sock:rw"
      - "/var/lib/docker:/var/lib/docker:rw"

Command Line

docker run \
  -v /var/run/docker.sock:/var/run/docker.sock:rw \
  -v /var/lib/docker:/var/lib/docker:rw \
  meltwater/docker-cleanup:latest

Kubernetes

You can find a Kubernetes DaemonSet configuration, that will allow you to run the meltwater/docker-cleanup container on every node of your cluster.

kubectl create -f contrib/k8s-daemonset.yml

Development

A Makefile is included to help with repetitive commands during development.

make help

More Repositories

1

served

A C++11 RESTful web server library
C++
709
star
2

drone-cache

A Drone plugin for caching current workspace files between builds to reduce your build times
Go
335
star
3

gen_rmq

Elixir AMQP consumer and publisher behaviours
Elixir
182
star
4

drone-convert-pathschanged

Drone conversion extension to include/exclude pipelines and pipeline steps based on paths changed
Go
133
star
5

secretary

DEPRECATED Secrets management for dynamic environments
Go
94
star
6

terraform-aws-asg-dns-handler

Terraform module for dynamically setting hostnames following a pattern on instances in AWS Auto Scaling Groups
Python
75
star
7

rabbitio

RabbitIO is used to move data in and out of RabbitMQ
Go
70
star
8

elasticsearch-batch-percolator

(deprecated) High performance Elasticsearch percolator
Java
48
star
9

lighter

DEPRECATED Marathon deployment automation tool
Python
43
star
10

awsudo

A simple utility for executing cli commands with an assumed role.
JavaScript
41
star
11

docker-mesos

Mesos, Marathon and Chronos using Docker Compose
Shell
39
star
12

jugri

The JUpyter-GRemlin Interface
Python
35
star
13

rxrabbit

RabbitMQ Java client built on RxJava
Java
33
star
14

ex_lsh

A configurable implementation of locality-sensitive hashing in Elixir
Elixir
25
star
15

proxymatic

DEPRECATED Marathon service discovery proxy
Python
24
star
16

quitsies

A persisted drop-in replacement for Memcached, respecting the rules of quitsies.
C++
16
star
17

api-gateway-demo

Nginx application showcasing how to do lightweight tests with ruby rspec
Ruby
15
star
18

kotlin-compose

Docker-compose wrapper for the JVM
Kotlin
12
star
19

rabbit-puppy

Configures RabbitMQ server based on YAML configuration
Kotlin
7
star
20

docker-elasticsearch-aws

Elasticsearch Docker image with the Amazon EC2 plugin
7
star
21

MeltwaterEng-public-presentations

List of public talks by Meltwater Engineering: meetup presentations, recorded conference talks, slides.
7
star
22

tau

Functional time and date JavaScript library.
JavaScript
6
star
23

puppet-marathon

Puppet Module for Mesos Marathon
Ruby
6
star
24

puppet-cobbler

Fork of https://bitbucket.org/jsosic/puppet-cobbler
HTML
5
star
25

puppet-incron

Puppet module to provide an incron resource type
Ruby
4
star
26

sparkline

ANSI Sparklines for Elixir
Elixir
4
star
27

supervisor_health

Elixir
4
star
28

meltwater-elasticsearch-queries

Java
3
star
29

phi

Functional tools for JavaScript inspired by Ramda and Ramda Adjunct.
JavaScript
3
star
30

confex_config_provider

Confex runtime configuration provider for Distillery
Elixir
3
star
31

vagrant-coreos

Vagrant infrastructure for CoreOS on Amazon AWS
HTML
3
star
32

underthehood-blog-tests-examples

Examples for blog post "Using Selenium-Server on Docker to run your Browser Tests"
Ruby
3
star
33

mlabs-health

Health monitor for Node.js microservices.
JavaScript
3
star
34

makenew-koa-service

Bootstrap a new Node.js Koa microservice in five minutes or less.
Shell
2
star
35

esi-include-webpack-plugin

Webpack 4 plugin to inject an esi comment for prod OR fetch and do the injection in dev
JavaScript
2
star
36

mlabs-aws

Convenient wrappers around the AWS SDK to keep code DRY.
JavaScript
2
star
37

threaded-logstasher

DEPRECATED Threaded logstash handler and formatter in Python.
Python
2
star
38

makenew-node-lib

Bootstrap a new Node.js npm package in five minutes or less.
Shell
2
star
39

docker-foundation-terraform

Repo to use when building terraforming docker image
2
star
40

node-examplr

Example runner for Node.js packages.
JavaScript
2
star
41

mlabs-graphql

Node.js GraphQL client and server toolkit.
JavaScript
2
star
42

docker-aws-cli

Custom Docker build of the AWS CLI
Dockerfile
1
star
43

the-orchard

A CLI tool to generate the markup for including web dependencies
JavaScript
1
star
44

puppet-erlang

puppet module to install erlang from source
Puppet
1
star
45

puppet-python

Installation of python from source via puppet
Puppet
1
star
46

mlabs-http

Node.js HTTP client wrapper around Got
JavaScript
1
star
47

puppet-monit

HTML
1
star
48

pagerduty-campfire

Polls Pagerduty for new stuff, posts it to Campfire
1
star
49

puppet-newrelic_agent

Puppet Module for Installing & Managing the various NewRelic server monitoring agents
Puppet
1
star
50

docker-nginx-http-https-reverse-proxy

Nginx HTTP server which proxies an external SSL host
Dockerfile
1
star
51

meltwater-ruby

(deprecated) Ruby Client for the Meltwater API
Ruby
1
star
52

terraform-provider-meltwater

A Terraform provider for Meltwater based on the persistent/infrastructure parts of the Meltwater API. Such as setting up webhooks and recurring exports.
Go
1
star
53

puppet-es

Puppet module for Elastic Search as a Defined Type
HTML
1
star
54

mlabs-logger

Structured logging for Node.js services.
JavaScript
1
star
55

mlabs-koa

Koa middleware suite and server bootstrapper.
JavaScript
1
star
56

docker-awscli

Docker image of Amazon AWS Command Line Interface
Shell
1
star
57

meltwater-killercoda

Meltwater Engineering's Public / Shared Training Center
Shell
1
star
58

meltwater-php

(deprecated) PHP Client for the Meltwater API
PHP
1
star
59

.github

Meltwater's community health files
1
star