We pull many docker images in real-life, but only a few people know how to manage the docker images. In this blog, we will learn about the docker system prune command.
Introduction to docker prune command
The Docker prune command removes all unused images, containers, volume, or network from the local storage. The docker system prune command follows the below syntax
docker system prune [OPTIONS]
The options can be
--all , -a : to remove all unused images not just dangling ones
--filter. : filter values (e.g. 'label=<key>=<value>')
--force , -f : to remove forcefully
--volumes : prune volumes
Is the docker system prune safe?
The docker system prune command is safe to use if you are aware of its usage. The docker prune commands will delete the following:-
- All stopped containers
- All network which is not used by any container
- All dangling image
- All build cache
Running the docker prune command will not execute until you press YES. It gives a warning and you have the option to either proceed or discard it.
Docker system prune images command
In this session, we will use the prune command to remove all stopped containers. Type the below command to verify the existing docker images, volume, and network.
docker image ls
docker volume ls
docker network ls
If you are new to docker then follow my below tutorial to understand docker images.
Docker system prune all command
Docker system prune all command is used to delete all stopped containers, networks, and images. Now type the prune command and verify the output.
docker system prune -a
Now again run the below command to verify if the prune command ran successfully
docker image ls
docker volume ls
docker network ls
NOTE: The above command will not delete the volume. To delete the volume user needs to pass the –volumes flag.
Docker system prune dry-run
In docker’s current version there is no dry-run option available. Maybe in the future docker community will add this future. For now, there is no way to test what all images, volume, and network prune commands will delete.
Docker volume prune
The docker volume prune command is used to delete all unused volumes. The user needs to pass the –volumes flag in the prune command to delete the volume. Before deleting the volume let’s list all the existing volumes.
docker volume ls
Now run the prune command to delete all unused volume
docker system prune -a --volumes
Now again run the volume command to verify if the prune command worked.
Conclusion
I hope you have liked this small tutorial about the docker system prune. In this blog, we have learned how to use the prune command to remove unused images, volume, and network.
Please do let me know if you are facing any issues while following along.