GithubHelp home page GithubHelp logo

jaakkytt / docker-gc Goto Github PK

View Code? Open in Web Editor NEW

This project forked from spotify/docker-gc

0.0 1.0 0.0 137 KB

Docker garbage collection of containers and images

License: Apache License 2.0

Shell 100.00%

docker-gc's Introduction

docker-gc

A simple Docker container and image garbage collection script.

  • Containers that exited more than an hour ago are removed.
  • Images that don't belong to any remaining container after that are removed.

Although docker normally prevents removal of images that are in use by containers, we take extra care to not remove any image tags (e.g., ubuntu:14.04, busybox, etc) that are in use by containers. A naive docker rmi $(docker images -q) will leave images stripped of all tags, forcing docker to re-pull the repositories when starting new containers even though the images themselves are still on disk.

This script is intended to be run as a cron job. You can also run it as a Docker container (see below).

Building

Build the debian package:

$ apt-get install git devscripts debhelper
$ git clone https://github.com/spotify/docker-gc.git
$ cd docker-gc
$ debuild -us -uc -b

If you get lintian errors during debuild, try debuild --no-lintian -us -uc -b.

Installing

$ dpkg -i ../docker-gc_0.0.4_all.deb

This installs the docker-gc script into /usr/sbin and drops a docker-gc cron file into /etc/cron.hourly/.

Usage

To use the script manually, run docker-gc. The system user under which docker-gc runs needs to have read and write access to the $STATE_DIR environment variable which defaults to /var/lib/docker-gc.

Excluding Images From Garbage Collection

There can be images that are large that serve as a common base for many application containers, and as such, make sense to pin to the machine, as many derivative containers will use it. This can save time in pulling those kinds of images. There may be other reasons to exclude images from garbage collection. To do so, create /etc/docker-gc-exclude, or if you want the file to be read from elsewhere, set the EXCLUDE_FROM_GC environment variable to its location. This file can contain image name patterns (in the grep sense), one per line, such as spotify/cassandra:latest or it can contain image ids (truncated to the length shown in docker images which is 12.

An example excludes file might contain:

spotify/cassandra:latest
9681260c3ad5

Excluding Containers From Garbage Collection

There can also be containers (for example data only containers) which you would like to exclude from garbage collection. To do so, create /etc/docker-gc-exclude-containers, or if you want the file to be read from elsewhere, set the EXCLUDE_CONTAINERS_FROM_GC environment variable to its location. This file should container name patterns (in the grep sense), one per line, such as mariadb-data.

An example excludes file might contain:

mariadb-data
drunk_goodall

Running as a Docker Image

A Dockerfile is provided as an alternative to a local installation. By default the container will start up, run a single garbage collection, and shut down.

Build the Docker Image

docker build -t spotify/docker-gc .

Running as a Docker Container

The docker-gc container requires access to the docker socket in order to function, so you need to map it when running, e.g.:

docker run --rm -v /var/run/docker.sock:/var/run/docker.sock spotify/docker-gc

docker-gc's People

Contributors

mbruggmann avatar rohansingh avatar rculbertson avatar davidxia avatar danielnorberg avatar drewcsillag avatar dangra avatar daenney avatar jaakkytt avatar jenserat avatar johnswanson avatar michel-kraemer avatar remohammadi avatar xealot avatar epipho avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.