Predictive Hacks

How to remove Docker Images and Containers

docker

Personally, I have found the official Docker Documentation not to be so helpful, especially for beginners. As I can see from the ratings, I am not the only one finds difficulties to follow it. For example, if I want to look at the documentation of how I can remove an image, at the bottom there is rate and guess what?

How to remove Docker Images and Containers 1

For that reason, I decided to write this tutorial of how you can remove Images and Containers in Docker.

How to Remove All Resources

If we want to remove any images, containers and volumes that are not associated with a container we can type:

$ docker system prune

And if we want to remove any unused images and stopped containers we can type:

$ docker system prune -a

How to Remove Docker Images

You can get all the images and their corresponding IDs and tags with the command:

$ docker images -a

Once, you get the IDs and the tags, you will be able to remove them using the command:

By tag:

$ docker rmi tag

By ID:

$ docker rmi ID

Note that we can remove many images in the same command by typing:

$ docker rmi ID1 ID2

If you want to get the list of the tangling images you can type:

$ docker images -f dangling=true

And you can remove them with the command:

$ docker image prune

Remove all images

If you want to remove all images you can type:

$ docker rmi $(docker images -a -q)

How to Remove Docker Containers

To get all the containers we type:

$ docker ps -a

And we can remove it by mentioning the ID or the Name

$ docker rm ID

Or

$ docker rm Name

Note that if we want to remove many containers we can type:

$ docker rm Name1 Name2 

The same with the IDs

Remove All Containers

If we want to remove all the containers, we first need to stop them and then to remove them as follows:

$ docker stop $(docker ps -a -q)
$ docker rm $(docker ps -a -q)

Resources

I found this tutorial from Digital Ocean very explanatory and to the point

Share This Post

Share on facebook
Share on linkedin
Share on twitter
Share on email

Leave a Comment

Subscribe To Our Newsletter

Get updates and learn from the best

More To Explore

snowflake
Miscellaneous

How to Schedule Tasks in Snowflake

We have started a series of Snowflake tutorials, like How to Get Data from Snowflake using Python, How to Load