Docker removing unused images and container
Tue Feb 27 2018
While learning docker and following through dozens of tutorials around the web, the amount of new images I downloaded and containers I created can easily be above the 50-60 scale…
The problem with that, is that most of them are not used!
most of them have the annoying “” in the image name, and all the containers are stopped.
I decided to start clearing stuff, so even though I would be happy to go one by one… I decided to batch the process, and here the 2 tiny commands that could help you as well.
remove all stopped containers:
docker rm $(docker ps -a | grep -v Up | awk '{print $1}')
how it works?
- ” docker ps -a ” – lists all containers
- ” grep -v Up ” select only the rows where the container is not “Up”
- ” awk ‘{print $1}’ ” take only the container id
- now after we took all the stopped container ids we simply remove them with the docker command.
remove all images with :
docker rmi $(docker images | grep "<none>" | awk '{print $3}')
how it works?
- ” docker images ” – list all images
- ” grep “” ” – take only the ones that contain
- ” awk ‘{print $3}’ ” – grab their ids
- now remove them with the docker command