{"id":196877,"date":"2021-03-08T17:00:01","date_gmt":"2021-03-08T14:00:01","guid":{"rendered":"https:\/\/en.buradabiliyorum.com\/how-to-prune-unused-docker-resources-cloudsavvy-it\/"},"modified":"2021-03-08T17:00:01","modified_gmt":"2021-03-08T14:00:01","slug":"how-to-prune-unused-docker-resources-cloudsavvy-it","status":"publish","type":"post","link":"https:\/\/buradabiliyorum.com\/en\/how-to-prune-unused-docker-resources-cloudsavvy-it\/","title":{"rendered":"#How to Prune Unused Docker Resources \u2013 CloudSavvy IT"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_85 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-6a3dd946739a7\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #dd3333;color:#dd3333\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #dd3333;color:#dd3333\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-6a3dd946739a7\" checked aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-prune-unused-docker-resources-cloudsavvy-it\/#Pruning_Dangling_Resources\" >Pruning Dangling Resources<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-prune-unused-docker-resources-cloudsavvy-it\/#Using_Filters_When_Pruning\" >Using Filters When Pruning<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-prune-unused-docker-resources-cloudsavvy-it\/#Pruning_Individual_Resource_Types\" >Pruning Individual Resource Types<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-prune-unused-docker-resources-cloudsavvy-it\/#Pruning_Containers\" >Pruning Containers<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-prune-unused-docker-resources-cloudsavvy-it\/#Pruning_Images\" >Pruning Images<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-prune-unused-docker-resources-cloudsavvy-it\/#Pruning_Networks\" >Pruning Networks<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-prune-unused-docker-resources-cloudsavvy-it\/#Pruning_Volumes\" >Pruning Volumes<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-prune-unused-docker-resources-cloudsavvy-it\/#Preventing_Future_Accumulation\" >Preventing Future Accumulation<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-prune-unused-docker-resources-cloudsavvy-it\/#Conclusion\" >Conclusion<\/a><\/li><\/ul><\/nav><\/div>\n<p><strong>&#8220;#How to Prune Unused Docker Resources \u2013 CloudSavvy IT&#8221;<\/strong><\/p>\n<div id=\"article-content-area\">\n<img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-9034\" src=\"https:\/\/www.cloudsavvyit.com\/thumbcache\/0\/0\/78cc37c3a66e5df9b2a9dc8cd64461c7\/p\/uploads\/2021\/01\/6dc7b5a0.jpeg\" alt=\"\" width=\"1602\" height=\"902\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>Docker lets you quickly package your <a href=\"https:\/\/buradabiliyorum.com\/en\/category\/download-scripts-themes-apps\/\" data-internallinksmanager029f6b8e52c=\"9\" title=\"Download Scripts &amp; Themes &amp; Apps\" target=\"_blank\" rel=\"noopener\">app<\/a>lications as containers, making it possible to run them anywhere from your laptop to a public cloud. After using Docker for a while, you can accumulate a large number of redundant containers, images and other resources. Here\u2019s how to tidy them up.<\/p>\n<p>Docker <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/docs.docker.com\/config\/pruning\">doesn\u2019t usually delete<\/a> anything unless you tell it to. That means images you\u2019ve pulled and containers you\u2019ve created are probably still hanging around on your system, even if they\u2019re no longer used.<\/p>\n<p>Having too many redundant resources can quickly lead to excessive disk usage. It also results in much lengthier output when listing resources on the command line, making it harder to find the information you\u2019re looking for.<\/p>\n<h2 id=\"pruning-dangling-resources\"><span class=\"ez-toc-section\" id=\"Pruning_Dangling_Resources\"><\/span>Pruning Dangling Resources<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>A \u201cdangling\u201d resource is any Docker object not currently associated with a container. This includes Docker images, networks and volumes, as well as stopped containers. In the case of images, an image is \u201cdangling\u201d if it\u2019s untagged and has no references.<\/p>\n<p>Docker provides a single housekeeping command to let you purge all dangling resources and stopped containers. Unless a resource is used by a <em>running<\/em> container, it will be deleted.<\/p>\n<pre><code>docker system prune<\/code><\/pre>\n<p>Running the command displays a confirmation prompt which lists the resource types to delete. Type <code>y<\/code> and press Enter to continue with the deletion. You can disable the confirmation by passing the <code>-f<\/code> or <code>--force<\/code> flag.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-10025\" src=\"https:\/\/www.cloudsavvyit.com\/thumbcache\/0\/0\/19221a1ffcc9ab3c87dcc30355397fda\/p\/uploads\/2021\/03\/d1a80701.png\" alt=\"\" width=\"955\" height=\"516\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>Volumes aren\u2019t removed by default. Deleting volumes risks the loss of persistent data which you might want to keep. If you do want to remove volumes which aren\u2019t associated with a container, pass the <code>--volumes<\/code> flag to include them in the operation.<\/p>\n<p>You can delete even more image data by passing the <code>-a<\/code> (or <code>--all<\/code>) flag. This will cause Docker to delete all <em>unused<\/em> images, not just dangling unreferenced ones. Each image on your system will be deleted, unless there\u2019s at least one running container that requires it.<\/p>\n<h2 id=\"using-filters-when-pruning\"><span class=\"ez-toc-section\" id=\"Using_Filters_When_Pruning\"><\/span>Using Filters When Pruning<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Filters can be used with the <code>prune<\/code> command. Filtering gives you more control over what gets deleted. Two filters are currently supported, <code>until<\/code> and <code>timestamp<\/code>.<\/p>\n<ul>\n<li><strong><code>until<\/code><\/strong> \u2013 The <code>until<\/code> filter prevents the deletion of containers, images and networks created before a given time. You can specify the time in several different formats, such as a Unix timestamp in seconds (e.g.\u00a0<code>1614890000<\/code>) or a date formatted timestamp string (e.g.\u00a0<code>2021-03-04T20:30:00<\/code>).<\/li>\n<li><strong><code>label<\/code><\/strong> \u2013 The <code>label<\/code> filter constrains the deletion so only resources with a given label are deleted. Labels can be used with containers, images, networks and volumes. You specify labels as either <code>label=key=value<\/code> (key equal to value), <code>label!=key=value<\/code> (key not equal to value), <code>label=key<\/code> (has the given label) or <code>label!=key<\/code> (does not have the given label).<\/li>\n<\/ul>\n<p>To use a filter, pass it to the <code>--filter<\/code> flag:<\/p>\n<pre><code>docker system prune --filter until=2021-03-04T20:30:00 --filter label=foo=bar<\/code><\/pre>\n<p>This command would delete resources with the <code>foo=bar<\/code> label that were created before 8:30pm on March 4th. Any resources not matching this criteria would be left untouched.<\/p>\n<h2 id=\"pruning-individual-resource-types\"><span class=\"ez-toc-section\" id=\"Pruning_Individual_Resource_Types\"><\/span>Pruning Individual Resource Types<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>You won\u2019t always need to use <code>docker system prune<\/code>. Docker provides more granular pruning commands too. These can be used when you only want to remove one type of resource.<\/p>\n<p>The <code>--force<\/code> and <code>--filter<\/code> flags supported by <code>docker system prune<\/code> also apply to the individual prune commands listed below.<\/p>\n<h3 id=\"pruning-containers\"><span class=\"ez-toc-section\" id=\"Pruning_Containers\"><\/span>Pruning Containers<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Day-to-day Docker commands can quickly result in a large number of stopped containers. The problem is exasperated if you <code>docker build<\/code> images without the <code>--rm<\/code> flag, as this causes inter<a href=\"https:\/\/buradabiliyorum.com\/en\/category\/social-mediaa\/\" data-internallinksmanager029f6b8e52c=\"1\" title=\"Social Media\" target=\"_blank\" rel=\"noopener\">media<\/a>ry build layers to be retained.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-10029\" src=\"https:\/\/www.cloudsavvyit.com\/thumbcache\/0\/0\/0eef2d7d830bcab93e6202c4e04a144b\/p\/uploads\/2021\/03\/bc995211.png\" alt=\"\" width=\"872\" height=\"147\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>Run <code>docker container prune<\/code> to clean up stopped containers. Stopped containers don\u2019t appear when you run <code>docker ps<\/code>; to see them, you\u2019ll need to use <code>docker ps -a<\/code> to show all the containers on your system.<\/p>\n<p>If you want to remove an individual container, use the <code>docker rm<\/code> command passing the container\u2019s ID. You can get this by running <code>docker ps<\/code>. If the container is running, you\u2019ll need to use the <code>--force<\/code> flag to delete it.<\/p>\n<h3 id=\"pruning-images\"><span class=\"ez-toc-section\" id=\"Pruning_Images\"><\/span>Pruning Images<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Use <code>docker image prune<\/code> to remove all dangling images. Like <code>docker system prune<\/code>, this will affect images that are either untagged or are not referenced by any container.<\/p>\n<p>Add the <code>-a<\/code> flag to instead delete all unused images. This will purge every image on your system that\u2019s not required by at least one container. Tagged images will be included too, letting you quickly clear up old pulls from Docker Hub.<\/p>\n<p>You can remove a specific image with the <code>docker rmi<\/code> command:<\/p>\n<pre><code>docker rmi wordpress:latest<\/code><\/pre>\n<p>When one image has multiple tags, <code>docker rmi<\/code> will remove the given tag without actually deleting the image. It will be deleted once the last tag has been removed.<\/p>\n<h3 id=\"pruning-networks\"><span class=\"ez-toc-section\" id=\"Pruning_Networks\"><\/span>Pruning Networks<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Docker networks aren\u2019t cleaned automatically. Over time, you can end up with a large number of them. This clutters your system with redundant virtual bridge devices and routing table entries. Run <code>docker network prune<\/code> to destroy any unused networks and revert the changes to your host\u2019s configuration.<\/p>\n<p>You can delete individual networks by running <code>docker rm my-network<\/code>.<\/p>\n<h3 id=\"pruning-volumes\"><span class=\"ez-toc-section\" id=\"Pruning_Volumes\"><\/span>Pruning Volumes<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Docker will never delete a volume. They store the persistent data created by your containers so unintended removal could have devastating consequences. Volumes may also be shared by multiple containers or left dormant ready to attach to another container in the future.<\/p>\n<p>If you want to prune unused volumes, you should run <code>docker volume prune<\/code>. This will delete any volumes which aren\u2019t used by at least one container. Individual volumes can be removed using <code>docker volume rm my-volume<\/code>.<\/p>\n<h2 id=\"preventing-future-accumulation\"><span class=\"ez-toc-section\" id=\"Preventing_Future_Accumulation\"><\/span>Preventing Future Accumulation<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>You can minimise the risk of redundant resources piling up in the future by taking care to pass the <code>--rm<\/code> flag to <code>docker build<\/code> and <code>docker run<\/code> commands.<\/p>\n<p>When used with <code>build<\/code>, it will cause the deletion of intermediary build layers (marked as <code>&lt;none&gt;:&lt;none&gt;<\/code> in <code>docker images<\/code>) output. This can represent a significant reduction in disk usage. A new intermediary layer is normally created for each instruction in your <code>Dockerfile<\/code>. You can usually remove them unless you intend to use them later as the starting point for a new image.<\/p>\n<p>Using <code>--rm<\/code> with <code>docker run<\/code> will remove the container as soon as its command exits. This is ideal when running a binary in a single-use container. Don\u2019t pass <code>--rm<\/code> to containers you intend to run perpetually, or that you might want to start and stop on-demand. Examples include web and database servers that run a service instead of a user-supplied command.<\/p>\n<h2 id=\"conclusion\"><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Pruning your Docker resources can free up disk space and help you work with the Docker CLI. Eliminating redundant containers and images makes it easier to identity the resources you\u2019re looking for.<\/p>\n<p>Docker won\u2019t delete anything unless you tell it to. It\u2019s worth running <code>docker system prune<\/code> periodically to keep your system clean. You could create a cron task that runs it monthly, using the <code>until<\/code> filter to create a retention period for recently added resources.\n<\/div>\n<blockquote><p><strong><span style=\"color: #ff6600;\">If you liked the article, do not forget to share it with your friends. Follow us on\u00a0<span style=\"color: #ff0000;\"><a style=\"color: #ff0000;\" href=\"https:\/\/news.google.com\/publications\/CAAqBwgKMLG0nwswvr63Aw\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Google News<\/a><\/span>\u00a0too, click on the star and choose us from your favorites.<\/span><\/strong><\/p><\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\">For forums sites go to <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/forum.buradabiliyorum.com\/\" target=\"_blank\" rel=\"noopener\">Forum.BuradaBiliyorum.Com<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\"><strong>If you want to read more like this article, you can visit our <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/en.buradabiliyorum.com\/technology\/\" target=\"_blank\" rel=\"noopener\">Technology category.<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<p><span style=\"color: black;\"><a style=\"color: #ff9900;\" href=\"https:\/\/www.cloudsavvyit.com\/10024\/how-to-prune-unused-docker-resources\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;#How to Prune Unused Docker Resources \u2013 CloudSavvy IT&#8221; Docker lets you quickly package your applications as containers, making it possible to run them anywhere from your laptop to a public cloud. After using Docker for a while, you can accumulate a large number of redundant containers, images and other resources. Here\u2019s how to tidy&#8230;<\/p>\n","protected":false},"author":1,"featured_media":196878,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"fifu_image_url":"https:\/\/www.cloudsavvyit.com\/thumbcache\/0\/0\/78cc37c3a66e5df9b2a9dc8cd64461c7\/p\/uploads\/2021\/01\/6dc7b5a0.jpeg","fifu_image_alt":"","footnotes":""},"categories":[18],"tags":[],"class_list":["post-196877","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-technology"],"_links":{"self":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/196877","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/comments?post=196877"}],"version-history":[{"count":0,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/196877\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media\/196878"}],"wp:attachment":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media?parent=196877"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/categories?post=196877"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/tags?post=196877"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}