How to make docker evict stuff
Stop docker slowly filling your disk
Docker is a popular local disk cache, masquerading as virtualisation technology.
But, unlike a real cache, Docker doesn't handle it's own "eviction" - it never deletes anything. Docker's disk usage just slowly grows and grows and often you first notice that when it fills your disk.
Eviction, of a kind
The best way, as far as I know, to get docker to do some eviction is by running:
docker system prune --all --force --filter "until=528h"
This "prunes" docker data.
--all- everything, not just "dangling" stuff--force- don't ask for confirmation--filter "until=528h"- older than 22 days (about 3 weeks)
I use 528h to ensure that the eviction doesn't align with
human periods (like the working week) which breaks up the eviction a
bit.
This isn't real eviction like memcache or redis would do - they are smarter and use "least recently used" (LRU) as the main metric for eviction. Unfortunately docker doesn't seem to record access times for it's layers/volumes/whatever so proper LRU is impossible. Instead, you just have to suffer the churn of everything occasionally getting deleted.
Automating it
No one wants "just one more thing to remember" so best to set the command to run automatically. Here's how with systemd:
# $HOME/.config/systemd/docker-prune.timer
[Unit]
Description=Prune docker data
[Timer]
OnCalendar=daily
# ensure it doesn't happen straight away at boot/resume from suspend:
RandomizedDelaySec=1h
Persistent=true
[Install]
WantedBy=timers.target
# $HOME/.config/systemd/docker-prune.timer
[Unit]
Description=Prune unused Docker data
Documentation=https://docs.docker.com/engine/reference/commandline/system_prune/
[Service]
Type=oneshot
ExecStart=/usr/bin/docker system prune --all --force --filter "until=528h"
Then you:
systemctl --user daemon-reload
systemctl --user enable docker-prune