How to Optimize Docker Performance With "docker system prune"?

Docker is a popular tool for containerizing applications, but if you're not careful, it can quickly eat up disk space and slow down your system. Fortunately, Docker provides you the docker system prune command that can help you reclaim disk space and optimize performance.

When you run containers in Docker, they create various artifacts such as images, volumes, and networks, which can accumulate over time and take up a significant amount of disk space. The docker system prune command removes these artifacts (that are not currently being used by any containers), freeing up disk space and improving performance.

To use docker system prune, simply run it in terminal like so:

docker system prune

This will prompt you to confirm if you want to delete the artifacts, and then it will remove:

  • All stopped containers;
  • All networks not used by at least one container;
  • All dangling images;
  • All dangling build cache.

You can also force-delete all unused artifacts without being prompted for confirmation, by using the -f (or --force) flag, for example, like so:

docker system prune -f

There are various other options you can use with this command, for example, to remove all unused volumes, remove all unused images, remove containers, images, networks, and volumes based on a filter, etc.

Please note that docker system prune only removes unused artifacts. If you have any containers that are using a particular image or volume, those artifacts will not be deleted. Additionally, be careful when using this command, as it will delete all unused artifacts, including ones that may be important to you.

In summary, docker system prune is a simple command that can help you optimize your Docker performance by removing unused artifacts and reclaiming disk space. By periodically running this command, you can ensure that your Docker system remains efficient, lean and performant.


This post was published by Daniyal Hamid. Daniyal currently works as the Head of Engineering in Germany and has 20+ years of experience in software engineering, design and marketing. Please show your love and support by sharing this post.