Sign inSign up

hence/cleanup

By hence

•Updated about 11 years ago

Image
0

626

hence/cleanup repository overview

⁠Hence Cleanup

Our cleanup image, with default KEEP_IMAGES environment settings

⁠From meltwater/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.

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.

⁠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.

⁠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 \
    --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"
⁠Command Line
docker run \
  -v /var/run/docker.sock:/var/run/docker.sock:rw \
  meltwater/docker-cleanup:latest

Tag summary

Content type

Image

Digest

sha256:33f67dfff…

Size

18.2 MB

Last updated

about 11 years ago

docker pull hence/cleanup