How to Manage Docker images on local disk

Manage Docker images on local disk

Still now we were discussed about docker and its some features like create a images, maintain process and remove. Now will see how to manage the docker images on local disk.

As docker images keep running which also keep store the log files in local disk. Which may cause the disk full issue in future. To overcome the issue, we should keep delete the log file with some certain times.

Here we discuss about how 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 “/”. Once method we can overcome the issue we can allocate the separate filesystem for docker logs. For that we need to create the separate file system using LVM and move or give symlink to /var/lib/docker.

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 removes 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 we 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.