Manage Docker images on local disk

Docker is very powerful containerization technique, and it is becoming famous in short time.
People are adapting and containerizing their applications for deployment.

Soon container become very heavy and consume too much of disk space, If you do not delete old images and layers you may soon run out of disk space.
Here i am trying to cover the ways to regain the disk space consumed by docker images.
  • Moving docker filesystem to bigger mount point.
Usually docker keeps all temporary files related to image building and layers at /var/lib/docker
This path is local to the system usually at root partition "/".
You can mount a bigger disk space and move the content of /var/lib/docker to the new mount location and make sym link. 
This way even docker images occupy space, will not affect  your system as it will be using some other mount location.

  • Remove Old docker images
Here are few ways to remove old and unused docker images
  • Removing stopped container
docker ps -a | grep Exited | awk '{print $1}' | xargs docker rm

Above command will remove the old stopped container.
  • If you do not want to remove all container you can have filter for days and weeks old like below
docker ps -a | grep Exited | grep "days ago" | awk '{print $1}' | xargs docker rm

docker ps -a | grep Exited | grep "weeks ago" | awk '{print $1}' | xargs docker rm
  • There are lot of dangling/intermediate layers which are created as part of docker image creation.
This is a great way to recover the spaces used by old and unused layers.
docker rmi $(docker images -f "dangling=true" -q)
  • Removing images of particular pattern For example
Here i am removing images which has SNAPSHOT with it.
docker rmi $(docker images | grep SNAPSHOT | awk '{print $3}')
  • Removing Weeks old images

docker images | grep "weeks ago" | awk '{print $3}' | xargs docker rmi

##Similarly You can remove days, months old images too.

Script can be found in following git repo.


Popular posts from this blog

Colour formatting - Jenkins Console

How to migrate Parent pom from maven to gradle