How To Remove All Docker Container Images?
Docker is very popular container technology. Docker is supported by Linux distributions and Windows operating systems. While using containers we may need to remove all container images from docker engine. We will look different ways to remove all docker container images in this tutorial.
List Docker Images
Before removing images we generally need to list existing images to be sure. This is a safety step to prevent catastrophic actions like checking before
dd command. We will use
sudo docker images . We use
sudo because we need
root privileges or be in
docker user group.
$ sudo docker images
Delete All Containers
Containers are instances created from the images. We can delete these instances or container images. We will use
docker ps command to list them and provide their UID to the
docker rm command which will delete given container.
$ sudo docker rm $(sudo docker ps -a -q)
As we can see that deleted container UID is printed to the terminal output.
Delete All Images
Images are pure data about the starting point of the containers. We generally download images from docker hub or create them and store for daily usage. If we do not need them we can delete then with
docker rmi command but we should provide images UID with the
docker images command like below.
$ sudo docker rmi -f $(sudo docker images -q)
Delete All Images Forcibly with Attached Volumes
If there are some issues while deleting docker images and volumes we may need to force them for removal. We will use
-f option to for remove all docker images and containers. We will also use
-v option for verbosity in conjunction with
$ sudo docker rm -vf $(sudo docker ps -a -q)
Delete All Unused Images and Volumes
Docker engine is an intellygent mechanism where is monitors image and volume usage. Most of the cases there will be unused images and volumes where they take storage. We can use
system prune to remove unused images and volumes. We can also put this command into
cron to made it automatic.
$ sudo docker system prune --all