Sub topic 14
Scheduling Cleanup Safely
This chapter assumes you can run basic Docker CLI commands and know the difference between an image and a container. You should also be comfortable with a Linux or macOS terminal, and you should have already used the individual prune commands. Here you will turn a manual cleanup into a scheduled one without risking data.
Why schedule a prune
Build servers and long-lived hosts collect stopped containers, dangling images, and build cache every day. If nobody cleans them up, the disk fills slowly until a build or a container fails with "no space left on device". A scheduled prune keeps usage flat, so you do not have to remember to run it.
Automation removes the confirmation prompt, so a scheduled command must be limited to what is safe to lose.
Choose a safe command
The command below removes stopped containers, unused networks, dangling images, and unused build cache that is older than seven days. The -f flag skips the confirmation prompt, which a script needs.
docker system prune -f --filter "until=168h"Two properties make this suitable for a schedule:
- It does not touch volumes. A plain
docker system prunenever removes volumes, and this command does not pass--volumes. - The
until=168hfilter keeps anything created in the last seven days. A container you stopped yesterday, or an image you built this week, survives.
Schedule it with cron
Cron is the standard job scheduler on Linux. This crontab entry runs the age-limited prune every Sunday at 03:00.
0 3 * * 0 docker system prune -f --filter "until=168h"The five fields are minute, hour, day of month, month, and day of week. Here 0 3 * * 0 means minute 0, hour 3, any day of month, any month, and Sunday.
Add it with crontab -e as a user who is allowed to run docker. The job runs with a minimal environment, so if cron cannot find the docker command, use the full path, which which docker shows.
Preview before you automate
Prune has no dry-run flag. Before you install the schedule, list what the command would target, using the same filters the prune uses internally.
docker image ls --filter dangling=true
docker ps -a --filter status=exited
docker volume ls --filter dangling=trueMeasure before and after as well. Compare docker system df output on the day you install the job and again after the first run. If reclaimable space does not drop, the job is not running or is not matching what you expected.
Common errors
- Job never runs. Cron's
PATHis short. Use the full path todocker, and check that the user is in thedockergroup. - A needed image disappears. Plain
docker system pruneremoves only dangling images. If you add-a, it also removes images not used by any container, and a weekly rebuild or pull may have to download them again. Leave-aout unless you accept that. - Disk still fills. Container logs are not removed by prune. Set log rotation in
/etc/docker/daemon.jsoninstead, as covered in the logs chapter. - Cleanup deletes something recent. Check that the
untilvalue is in a unit Docker accepts, such as168h. Days are not a valid unit here.
Exercise
A teammate proposes this weekly cron job: docker system prune -a -f --volumes. Name two reasons it is unsafe, and give the command you would schedule instead.
Show answer
The --volumes flag deletes volumes that no container currently uses, which can destroy database or application data. The -a flag removes every image not used by a container, and there is no until filter, so recent images are removed too. A safer command is docker system prune -f --filter "until=168h", which skips volumes and keeps anything newer than seven days.