Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

How to clear Docker cache and free up space on your system

Posted on Aug 8 • Originally published at depot.dev Docker persists build cache, containers, images, and volumes to disk. Over time, these things can take up a lot of space on your system. In this post, we'll look at the different Docker artifacts that can take up space on your system, how to clear them individually, and using docker system prune to clear Docker cache.The first step is knowing the disk usage of Docker. We can use the docker system df command to get a breakdown of how much disk space is being taken up by various artifacts.Docker uses 36.18GB for images, 834.8kB for containers, 15.31GB for local volumes, and 1.13GB for the docker build cache. About 50 GB of space in total, and a large chunk of it is reclaimable.We can use the docker container prune command to clear the disk space used by containers. This command will remove all stopped containers from the system.We can omit the -f flag here and in subsequent examples to get a confirmation prompt before artifacts are removed.We can see the ids of unused containers by running the docker ps command with filters on the status of the container. A container is unused if it's in status exited or dead.Note: If we want to know the size of the unused container, we can replace the -q flag with -s to get the size and other metadata about the container.If we want to remove all containers from the system, we can stop any running containers and then use the same prune command. We do this by feeding the output of docker ps -q into the docker stop command or docker kill command if you want to kill the container forcibly.Another option is the docker rm command which can be used with docker ps -a -q to remove all containers.Note: The docker rm command forces the removal of a running container via a SIGKILL signal. This is the same as the docker kill command. The docker ps -a -q command will list all containers on the system, including running containers, and feed that into the docker rm command.Docker images can take up a significant amount of disk space. We accumulate new images when base images change or build new ones via docker build, etc. We can use the docker image prune command to remove unused images from the system.By default, it only removes dangling images. An image that is not associated with any container and doesn't have a tag.We reclaimed 2.7GB of space by removing dangling images. But, if we recall from our docker system df command, we have 34.15GB of reclaimable images.Where is the rest of that space coming from? These are images on our system that are tagged or associated with a container. We can run the docker image prune- a command to force the removal of these images as well, assuming they're unused images.We remove all unused images not associated with a container, not just the dangling ones.Volumes are never cleaned up automatically in Docker because they could contain valuable data. But, if we know that we no longer need the data in a volume, we can remove it with the docker volume prune command. This removes all anonymous volumes not used by any containers.Interestingly, we see that we didn't reclaim any space. This is because we have volumes that are associated with containers. We can see these volumes by running the docker volume ls command.We get an output that shows the driver and the volume name. The command docker volume pruneonly removes anonymous volumes. These volumes are not named and don't have a specific source from outside the container. We can use the docker volume rm -a command to remove all volumes.To remove the Docker build cache, we can run the docker buildx prune command to clear the build cache of the default builder.If we want to remove the build cache for a specific builder, we can use the --builder flag to specify the builder name.While Docker networks don't take up disk space on our machine, they do create network bridges, iptables, and routing table entries. So similarly to the other artifacts, we can remove unused networks with the docker network prune command to clean these up.We can remove all unused artifacts Docker has produced by running docker system prune. This will remove all unused containers, images, networks and build cache.By default, this command will not remove volumes and only removes dangling Docker images. We can use the --volumes flag to remove volumes as well. We can also add the -a flag again to remove all images not associated with a container.Docker consists of different artifacts like containers, images, volumes, build cache, and even networks. These artifacts, over time, can chew up valuable resources, particularly disk space. It's handy to know how to use the individual prune commands and the docker system prune command to clean up these artifacts and free up disk space on your system.Templates let you quickly answer FAQs or store snippets for re-use. Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink. Hide child comments as well Confirm For further actions, you may consider blocking this person and/or reporting abuse Tanvir Rahman - Jun 22 popoola Temitope - Jun 22 Goon Nguyen - Jun 23 Jin Lee - Jun 22 Once suspended, kylegalbraith will not be able to comment or publish posts until their suspension is removed. Once unsuspended, kylegalbraith will be able to comment and publish posts again. Once unpublished, all posts by kylegalbraith will become hidden and only accessible to themselves. If kylegalbraith is not suspended, they can still re-publish their posts from their dashboard. Note: Once unpublished, this post will become invisible to the public and only accessible to Kyle Galbraith. They can still re-publish the post if they are not suspended. Thanks for keeping DEV Community safe. Here is what you can do to flag kylegalbraith: kylegalbraith consistently posts content that violates DEV Community's code of conduct because it is harassing, offensive or spammy. Unflagging kylegalbraith will restore default visibility to their posts. DEV Community — A constructive and inclusive social network for software developers. With you every step of your journey. Built on Forem — the open source software that powers DEV and other inclusive communities.Made with love and Ruby on Rails. DEV Community © 2016 - 2023. We're a place where coders share, stay up-to-date and grow their careers.



This post first appeared on VedVyas Articles, please read the originial post: here

Share the post

How to clear Docker cache and free up space on your system

×

Subscribe to Vedvyas Articles

Get updates delivered right to your inbox!

Thank you for your subscription

×