Module 13 Lesson 3: System Maintenance
Reclaim your disk space. Master the art of Docker housekeeping, from pruning dangling images to managing large log files that eat your server's storage.
Module 13 Lesson 3: System Maintenance
Docker is a "Storage Hungry" system. It keeps every image layer, every volume, and every log file forever unless you tell it otherwise. A healthy developer machine or production server needs regular maintenance.
1. The "Prune" Hierarchy
The prune command is your main weapon.
docker container prune: Deletes all stopped containers. (Low risk).docker network prune: Deletes all unused networks. (Low risk).docker image prune: Deletes only "Dangling" images (those with no name/tag). (Medium risk).docker system prune: Deletes containers, networks, and dangling images.docker system prune -a: Deletes EVERYTHING not currently tied to a running container. (High risk—you'll have to download your images again).
2. Managing the Log Bloat
Every time a container prints a line of code, Docker saves it to a .json file on your hard drive. If you have a chatty app, these logs can grow to 50GB!
The Global Fix (Docker Desktop):
In your settings, find the "Log rotation" section and limit individual log files to 10MB.
The Docker Compose Fix:
services:
app:
# ...
logging:
driver: "json-file"
options:
max-size: "10m"
max-file: "3"
(This keeps only the last 3 files of 10MB each, ensuring your app never uses more than 30MB for logs).
3. High-Performance Maintenance: df and dui
Docker provides its own disk usage tool:
docker system df
It shows you a summary of how much space is used by images, containers, and volumes, and how much is "Reclaimable" (not in use).
Exercise: The Big Clean
- Run
docker system df. How much "Reclaimable" space do you have? - Run
docker system prune(without the-a). - Check
docker system dfagain. How much space did you save? - Navigate to your newest project. Add the "Logging" block from Section 2 to your
docker-compose.yml. - Why is it dangerous to run a production server without log rotation? (Research: "Log file disk exhaustion").
Summary
Regular maintenance is what separates a "Brittle" system from a "Resilient" one. By automating your prunes and setting strict limits on your log files, you ensure that your servers stay up 24/7 without needing manual intervention to clear disk space.
Next Lesson: Final checks: Docker security checklist for production.