Docker prune vs system prune: Key Differences and Usage
docker prune command removes unused Docker objects like containers, networks, or volumes selectively, while docker system prune cleans up all unused objects including images, containers, networks, and optionally volumes in one command. docker system prune is a broader cleanup tool compared to specific docker prune commands.Quick Comparison
This table summarizes the key differences between docker prune commands and docker system prune.
| Feature | docker prune (e.g., container prune) | docker system prune |
|---|---|---|
| Scope | Cleans specific unused objects (containers, networks, volumes, or images) | Cleans all unused objects at once (containers, networks, images, optionally volumes) |
| Command Variants | docker container prune, docker network prune, docker volume prune, docker image prune | Single command: docker system prune |
| Volume Cleanup | Only with docker volume prune | Optional with --volumes flag |
| Confirmation Prompt | Yes, asks before deleting | Yes, asks before deleting |
| Use Case | Selective cleanup of one resource type | Broad cleanup of all unused resources |
| Risk Level | Lower risk, targets one resource type | Higher risk, can remove more data |
Key Differences
The docker prune commands are specific to resource types. For example, docker container prune removes only stopped containers, while docker network prune removes unused networks. This lets you clean up one type of resource without affecting others.
In contrast, docker system prune is an all-in-one cleanup command that removes all unused containers, networks, images, and optionally volumes with a single command. It is useful when you want to free up space quickly by removing all unused Docker objects.
Another difference is volume cleanup. Volumes are not removed by default in docker system prune unless you add the --volumes flag. With docker volume prune, only unused volumes are removed. Both commands ask for confirmation before deleting to prevent accidental data loss.
Code Comparison
Here is how you remove all stopped containers using docker container prune:
docker container prune # You will be prompted to confirm deletion of all stopped containers.
docker system prune Equivalent
To remove all unused containers, networks, images, and optionally volumes, use docker system prune:
docker system prune --volumes # You will be prompted to confirm deletion of all unused objects including volumes.
When to Use Which
Choose docker prune commands when you want to clean up a specific type of Docker resource without affecting others. For example, use docker container prune to remove only stopped containers safely.
Choose docker system prune when you want a quick, broad cleanup of all unused Docker objects to free up space. Add the --volumes flag if you also want to remove unused volumes, but be cautious as this can delete important data.
Key Takeaways
docker prune commands clean specific unused Docker resources selectively.docker system prune cleans all unused Docker objects in one go.docker system prune --volumes to also remove unused volumes.