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.
alpine:latest with bash, docker-cli, docker-cli-buildx, jq, curl, coreutils, findutils, and grep.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.| Order | Script | Capability | Default |
|---|---|---|---|
00a | 00a-nl-policy-compiler.sh | Natural-language cleanup policy compiler (NL_POLICY_ENABLED=true) | Opt-in |
00b | 00b-dry-run-audit.sh | Pre/post docker system df baseline & dry-run audit | Enabled |
00c | 00c-disk-pressure-guard.sh | Kubelet-style disk watermark adaptive mode (LOW/HIGH/CRITICAL_WATERMARK) | Enabled (WATERMARK_MODE=false) |
00d | 00d-predictive-eviction.sh | Linear-regression disk exhaustion forecaster (PREDICTIVE_EVICTION_ENABLED=true) | Opt-in |
00e | 00e-volume-quarantine.sh | Snapshot-before-delete .tar.gz volume safety net (VOLUME_QUARANTINE=true) | Opt-in |
01 | 01-volumes.sh | Dangling Docker volumes (native or legacy fallback) | Enabled |
02 | 02-exited-containers.sh | Exited and dead containers | Enabled |
03 | 03-created-containers.sh | Containers stuck in created state across DELAY_TIME | Enabled |
03b | 03b-smart-image-retention.sh | N-recent per-repo & tag-pattern image retention (KEEP_RECENT_PER_REPO) | Configurable (0) |
04 | 04-images.sh | Unused Docker images (honors KEEP_IMAGES, keep label, 03b retention) | Enabled |
05 | 05-buildx-cache.sh | BuildKit/Buildx cache LRU & age pruning (BUILD_CACHE_UNTIL, BUILD_CACHE_KEEP_STORAGE) | Enabled |
06 | 06-container-logs.sh | Zero-downtime truncate -s 0 on oversized *-json.log files (MAX_CONTAINER_LOG_SIZE) | Enabled (needs /var/lib/docker) |
07 | 07-networks.sh | Two-phase unused custom Docker network pruner (KEEP_NETWORKS) | Enabled |
08 | 08-docker-tmp.sh | Orphaned /var/lib/docker/tmp pull/load/export artifact cleaner | Enabled (needs /var/lib/docker) |
09 | 09-compose-orphans.sh | Fully stopped Docker Compose stack cleaner (CLEAN_COMPOSE_ORPHANS=true) | Opt-in |
11 | 11-label-ttl-cleaner.sh | Label-driven docker-cleanup.ttl=<duration> reaper across resources | Enabled |
13 | 13-zombie-health-reaper.sh | Unhealthy / OOMKilled / crash-loop reaper with crash log dump (REAP_UNHEALTHY=true) | Opt-in |
14 | 14-buildx-builders.sh | Named docker-container Buildx builder cache & idle cleaner (CLEAN_BUILDX_BUILDERS=true) | Opt-in |
15 | 15-swarm-cleaner.sh | Swarm manager unreferenced configs & secrets cleaner | Enabled (Swarm manager only) |
16 | 16-inode-overlay-guard.sh | Inode exhaustion guard & top-inode overlay2 container evictor (INODE_HIGH_WATERMARK) | Enabled |
17 | 17-cve-image-cleaner.sh | Trivy CVE CRITICAL unused image priority cleaner (CLEAN_CVE_IMAGES=true) | Opt-in |
20 | 20-multi-runtime-cleaner.sh | Podman & containerd (nerdctl/crictl) adapter (MULTI_RUNTIME_ENABLED=true) | Opt-in |
98 | 98-metrics-exporter.sh | Prometheus textfile exporter (PROMETHEUS_TEXTFILE_DIR) & WEBHOOK_URL summary | Enabled |
99 | 99-ai-bloat-advisor.sh | AI-native sanitized disk bloat root-cause advisor (AI_ADVISOR_ENABLED=true) | Opt-in |
prepare and execute phases.true to simulate all cleaners with zero destructive actions.false to run a single cleanup cycle and exit.1 for verbose debug output.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.*:* to keep all images (never a bare *). Use **All** for KEEP_CONTAINERS / KEEP_CONTAINERS_NAMED.03b-smart-image-retention.sh)."latest, v").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
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
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
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).
Content type
Image
Digest
sha256:de0e8f6fb…
Size
43.2 MB
Last updated
1 day ago
docker pull thanhlvcom/docker-cleanup