How to remove unused Docker resources?
· Category: Docker
Short answer
Docker provides prune commands to remove unused containers, images, networks, and volumes. docker system prune cleans all dangling resources, while specific prune commands target individual resource types.
Steps
- Remove stopped containers:
docker container prune. - Remove dangling images:
docker image prune. - Remove unused networks:
docker network prune. - Remove unused volumes:
docker volume prune. - Clean everything at once:
docker system prune -a --volumes.
Example
docker container prune -f
docker image prune -f
docker volume prune -f
docker system prune -a --volumes -f
Schedule a cron job for periodic cleanup:
0 2 * * * /usr/bin/docker system prune -f
Tips
- Always review what will be deleted before running prune commands in production.
- Use filters to prune resources older than a specific time:
docker system prune --filter "until=24h"
- Back up important volume data before pruning volumes.
Common issues
- Pruning volumes is irreversible and can delete important data.
- Running containers are never removed by prune commands.
- The
-aflag removes all unused images, not just dangling ones.