HUB
Sign inSign up

thanhlvcom/docker-cleanup

By thanhlvcom

•Updated 1 day ago

Image
0

161

thanhlvcom/docker-cleanup repository overview

⁠Docker Cleanup

Fork Notice: This repository is a fork of MeltwaterArchive/docker-cleanup⁠ (which has been archived and is no longer maintained). Development and maintenance continue here with modernized base images, refactored modular cleanup scripts, and 20 new WOW cleaners.

This image periodically cleans up exited/dead/stuck containers and removes Docker images, volumes, build caches, oversized logs, unused networks, and orphaned resources that are not in use by running containers. Originally based on tutumcloud/image-cleanup⁠ and chadoe/docker-cleanup-volumes⁠, and extended from MeltwaterArchive/docker-cleanup⁠.

WARNING: This script will remove all exited containers, data-only containers, and unused images unless you carefully exclude them (via KEEP_* patterns or docker-cleanup.keep=true labels). Take care if you mount /var/lib/docker into the container. Test with DRY_RUN=true first on production hosts.

⁠What's New in This Fork

  • Modernized Alpine image: Built on alpine:latest with bash, docker-cli, docker-cli-buildx, jq, curl, coreutils, findutils, and grep.
  • Two-phase modular cleaner architecture (cleaners/*.sh): Every cleaner supports --phase prepare|execute|all, global DRY_RUN=true, docker-cleanup.keep=true label protection, ONLY_CLEANERS/SKIP_CLEANERS filtering, and fault-isolated execution.
  • Full Suite of 24 Modular Cleaners (4 Baseline + 20 WOW Cleaners) — see docs/wow-cleaners-discovery-report.md⁠ for the full design rationale:
OrderScriptCapabilityDefault
00a00a-nl-policy-compiler.shNatural-language cleanup policy compiler (NL_POLICY_ENABLED=true)Opt-in
00b00b-dry-run-audit.shPre/post docker system df baseline & dry-run auditEnabled
00c00c-disk-pressure-guard.shKubelet-style disk watermark adaptive mode (LOW/HIGH/CRITICAL_WATERMARK)Enabled (WATERMARK_MODE=false)
00d00d-predictive-eviction.shLinear-regression disk exhaustion forecaster (PREDICTIVE_EVICTION_ENABLED=true)Opt-in
00e00e-volume-quarantine.shSnapshot-before-delete .tar.gz volume safety net (VOLUME_QUARANTINE=true)Opt-in
0101-volumes.shDangling Docker volumes (native or legacy fallback)Enabled
0202-exited-containers.shExited and dead containersEnabled
0303-created-containers.shContainers stuck in created state across DELAY_TIMEEnabled
03b03b-smart-image-retention.shN-recent per-repo & tag-pattern image retention (KEEP_RECENT_PER_REPO)Configurable (0)
0404-images.shUnused Docker images (honors KEEP_IMAGES, keep label, 03b retention)Enabled
0505-buildx-cache.shBuildKit/Buildx cache LRU & age pruning (BUILD_CACHE_UNTIL, BUILD_CACHE_KEEP_STORAGE)Enabled
0606-container-logs.shZero-downtime truncate -s 0 on oversized *-json.log files (MAX_CONTAINER_LOG_SIZE)Enabled (needs /var/lib/docker)
0707-networks.shTwo-phase unused custom Docker network pruner (KEEP_NETWORKS)Enabled
0808-docker-tmp.shOrphaned /var/lib/docker/tmp pull/load/export artifact cleanerEnabled (needs /var/lib/docker)
0909-compose-orphans.shFully stopped Docker Compose stack cleaner (CLEAN_COMPOSE_ORPHANS=true)Opt-in
1111-label-ttl-cleaner.shLabel-driven docker-cleanup.ttl=<duration> reaper across resourcesEnabled
1313-zombie-health-reaper.shUnhealthy / OOMKilled / crash-loop reaper with crash log dump (REAP_UNHEALTHY=true)Opt-in
1414-buildx-builders.shNamed docker-container Buildx builder cache & idle cleaner (CLEAN_BUILDX_BUILDERS=true)Opt-in
1515-swarm-cleaner.shSwarm manager unreferenced configs & secrets cleanerEnabled (Swarm manager only)
1616-inode-overlay-guard.shInode exhaustion guard & top-inode overlay2 container evictor (INODE_HIGH_WATERMARK)Enabled
1717-cve-image-cleaner.shTrivy CVE CRITICAL unused image priority cleaner (CLEAN_CVE_IMAGES=true)Opt-in
2020-multi-runtime-cleaner.shPodman & containerd (nerdctl/crictl) adapter (MULTI_RUNTIME_ENABLED=true)Opt-in
9898-metrics-exporter.shPrometheus textfile exporter (PROMETHEUS_TEXTFILE_DIR) & WEBHOOK_URL summaryEnabled
9999-ai-bloat-advisor.shAI-native sanitized disk bloat root-cause advisor (AI_ADVISOR_ENABLED=true)Opt-in

⁠Environment Variables

⁠Core Settings
  • CLEAN_PERIOD=1800 - Interval in seconds between cleanup cycles.
  • DELAY_TIME=1800 - Seconds to wait between prepare and execute phases.
  • DRY_RUN=false - Set to true to simulate all cleaners with zero destructive actions.
  • LOOP=true - Set to false to run a single cleanup cycle and exit.
  • DEBUG=0 - Set to 1 for verbose debug output.
  • DOCKER_SOCKET=/var/run/docker.sock - Path to the Docker socket.
  • DOCKER_ROOT=/var/lib/docker - Path to host Docker data root inside the container.
  • ONLY_CLEANERS / SKIP_CLEANERS - Comma-separated cleaner filename prefixes to include or exclude.
⁠Protection & Retention
  • Label contract: Add docker-cleanup.keep=true to any container, image, volume, or network to protect it from all cleaners. Add docker-cleanup.ttl=24h to auto-reap expired resources.
  • KEEP_IMAGES / KEEP_CONTAINERS / KEEP_CONTAINERS_NAMED / KEEP_VOLUMES / KEEP_NETWORKS - Comma-separated left-anchored bash shell pattern lists (NOT regexes). Use *:* to keep all images (never a bare *). Use **All** for KEEP_CONTAINERS / KEEP_CONTAINERS_NAMED.
  • KEEP_RECENT_PER_REPO=0 - Keep the N most recent images per repository (03b-smart-image-retention.sh).
  • KEEP_TAG_PATTERNS - Comma-separated tag prefixes to retain (e.g. "latest, v").
⁠Advanced Cleaner Settings
  • BUILD_CACHE_UNTIL=72h, BUILD_CACHE_KEEP_STORAGE - BuildKit/Buildx cache retention.
  • MAX_CONTAINER_LOG_SIZE=200M, KEEP_LOGS_CONTAINERS - Threshold & exclusions for zero-downtime log truncation.
  • WATERMARK_MODE=false, LOW_WATERMARK=70, HIGH_WATERMARK=80, CRITICAL_WATERMARK=90, AGGRESSIVE_DELAY_TIME=5 - Kubelet-style adaptive disk pressure guard.
  • VOLUME_QUARANTINE=false, QUARANTINE_DIR=/tmp/docker-cleanup-quarantine, QUARANTINE_TTL_DAYS=7 - Snapshot-before-delete volume backup.
  • PROMETHEUS_TEXTFILE_DIR, WEBHOOK_URL - Observability and Slack/Discord/JSON notifications.
  • AI_ADVISOR_ENABLED=false, NL_POLICY_ENABLED=false, AI_API_BASE_URL, AI_API_KEY, AI_MODEL - Optional OpenAI-compatible/Ollama integrations.

⁠Deployment

Example docker-compose.yml (see the full docker-compose.yml⁠ in the repository⁠ for all available options):

services:
  docker-cleanup:
    image: thanhlvcom/docker-cleanup:latest
    # Or build locally from this repository:
    # build: .
    container_name: docker-cleanup
    restart: unless-stopped
    labels:
      - "docker-cleanup.keep=true"
    environment:
      # Core schedule & safety
      CLEAN_PERIOD: "1800"
      DELAY_TIME: "1800"
      DRY_RUN: "${DRY_RUN:-false}"
      DOCKER_ROOT: "/var/lib/docker"

      # Protection & smart retention
      KEEP_RECENT_PER_REPO: "3"
      KEEP_TAG_PATTERNS: "latest,v"

      # Core cleaners
      CLEAN_BUILD_CACHE: "true"
      BUILD_CACHE_UNTIL: "72h"
      BUILD_CACHE_KEEP_STORAGE: "10GB"
      CLEAN_CONTAINER_LOGS: "true"
      MAX_CONTAINER_LOG_SIZE: "200M"
      CLEAN_NETWORKS: "true"
      CLEAN_DOCKER_TMP: "true"
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock:rw
      - /var/lib/docker:/var/lib/docker:rw
    logging:
      driver: json-file
      options:
        max-size: "10m"
        max-file: "3"
# Dry-run preview first
DRY_RUN=true docker compose up --build

# Run in background
docker compose up -d --build
⁠Command Line (Dry-Run Preview First)
docker run --rm \
  -e DRY_RUN=true \
  -e LOOP=false \
  -e DELAY_TIME=5 \
  -v /var/run/docker.sock:/var/run/docker.sock:rw \
  -v /var/lib/docker:/var/lib/docker:rw \
  meltwater/docker-cleanup:latest
⁠Daemon Mode
docker run -d \
  --name docker-cleanup \
  -e KEEP_RECENT_PER_REPO=3 \
  -e BUILD_CACHE_UNTIL=72h \
  -v /var/run/docker.sock:/var/run/docker.sock:rw \
  -v /var/lib/docker:/var/lib/docker:rw \
  meltwater/docker-cleanup:latest

⁠Development & Testing

make help
make test

You can also run any cleaner standalone:

DRY_RUN=true ./cleaners/05-buildx-cache.sh --phase all

To add a new cleaner, place an executable *.sh script inside cleaners/ (files prefixed with _ are shared libraries and skipped by run.sh).

⁠Upstream & License

Tag summary

Content type

Image

Digest

sha256:de0e8f6fb…

Size

43.2 MB

Last updated

1 day ago

docker pull thanhlvcom/docker-cleanup