A Docker host keeps filling its disk. Write a safe cleanup approach.
BeginnerScenarioScenarios: backups, log rotation and housekeeping
Answer
Situation read → Images, stopped containers, build cache and container logs pile up. Cleanup must not remove what running workloads need.
What I'd do →
- Measure:
docker system df -v. - Remove only unused objects older than a threshold:
docker container prune -f --filter 'until=24h' docker image prune -af --filter 'until=168h' docker builder prune -f --filter 'until=72h' - Avoid
docker volume prunein automation unless you are sure; volumes hold data. - Cap container logs in
/etc/docker/daemon.json:{"log-driver":"json-file","log-opts":{"max-size":"10m","max-file":"3"}}. - Run the prune from a weekly systemd timer and alert on disk usage.
Why → Time filters keep recently used images for fast rollbacks; log caps stop the most common runaway growth.
Interview tip → Mention that docker system prune -a --volumes is a data-loss command, not a routine one.
More on Scenarios: backups, log rotation and housekeeping
Design a nightly backup of `/etc` and application data to S3 with retention.IntermediateWrite the core of a Postgres backup that streams `pg_dump` through gzip to S3 and never uploads an empty dump.IntermediateBackups have "succeeded" every night for a year, but the first real restore failed. How do you make backups trustworthy?Advanced`/var` is 100% full because an app's logs were never rotated, and the app now fails to start. What do you do now, and what do you set up after?BeginnerShould you use `copytruncate` or a `postrotate` signal for an application's logs? How do you decide?Intermediate`/srv/app/releases` has 300 old release directories. Write a cleanup that keeps the newest 5 and never deletes the current release.Beginner