Interview Prep/Shell Scripting/Scenarios: backups, log rotation and housekeeping

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 →

  1. Measure: docker system df -v.
  2. 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'
    
  3. Avoid docker volume prune in automation unless you are sure; volumes hold data.
  4. Cap container logs in /etc/docker/daemon.json: {"log-driver":"json-file","log-opts":{"max-size":"10m","max-file":"3"}}.
  5. 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.

All Shell Scripting questions Roadmap: Shell scripting (Bash)

More on Scenarios: backups, log rotation and housekeeping