<?xml version="1.0" encoding="UTF-8"?><rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <channel>
    <title>docker &amp;mdash; alan</title>
    <link>https://www.franzoni.eu/alan/tag:docker</link>
    <description>Alan&#39;s musings</description>
    <language>en</language>
    <pubDate>Tue, 17 Nov 2026 00:40:54 +0000</pubDate>
    <item>
      <title>Automated cleanup of Docker leavings</title>
      <link>https://www.franzoni.eu/alan/cleaning-up-docker-leavings</link>
      <description>&lt;![CDATA[&#xA;&#xA;As much as I like Docker, one thing it doesn&#39;t do well: cleanup whatever is left on my hard drive after running it for a while. There&#39;re generally two kinds of Docker Waste:&#xA;&#xA;Exited containers. Unless you used run --rm when running a new container from an image, docker doesn&#39;t automagically remove containers once they&#39;re stopped; you need to rm them explicitly. Since it&#39;s generally considered (at least, by me) a Docker good practice to never restart a container, they just stay there and take valuable disk space. Of course sometimes it&#39;s useful or even necessary not to start a container with --rm, because you may want to inspect the filesystem after exiting, but most of the times it&#39;s an oversight.&#xA;Dangling images. Whenever you build a brand-new Docker image with the same tag as an existing one, you&#39;ll be possibly left with an old, dangling image - i.e an image you can only use by tag, not by name. The same could happen when an upstream image changes. More or less just like a git dangling object. Small note: it doesn&#39;t necessarily happen. The new image can be a perfect child of the previous image, in that case Docker uses a kind of inheritance mechanism which produces no dangling images. But if the image changes &#34;in the middle&#34; of the Dockerfile, and hence a piece of the old image get invalidated, then you&#39;ll be left with some dangling images.&#xA;&#xA;So, here&#39;s the script. Just put it in your /etc/cron.daily directory and live quite happily ever after:&#xA;&#xA;!/bin/bash&#xA;docker ps -a -q --filter=&#34;status=created&#34; | xargs --no-run-if-empty \&#xA;docker rm&#xA;docker ps -a -q --filter=&#34;status=exited&#34; | xargs --no-run-if-empty \&#xA;docker rm&#xA;docker images -q -f dangling=true | xargs --no-run-if-empty \&#xA;docker rmi&#xA;&#xA;#MostlyUnixish #docker&#xA;]]&gt;</description>
      <content:encoded><![CDATA[<p><img src="https://files.franzoni.eu/import/alanslair/2016/09/1619438_76bcbb30.jpg" alt=""></p>

<p>As much as I like <a href="https://www.docker.com/" rel="nofollow">Docker</a>, one thing it doesn&#39;t do well: cleanup whatever is left on my hard drive after running it for a while. There&#39;re generally two kinds of Docker Waste:</p>
<ul><li>Exited containers. Unless you used <code>run --rm</code> when running a new container from an image, docker doesn&#39;t automagically remove containers once they&#39;re stopped; you need to <code>rm</code> them explicitly. Since it&#39;s generally considered (at least, by me) a Docker good practice to <em>never restart a container</em>, they just stay there and take valuable disk space. Of course sometimes it&#39;s useful or even necessary not to start a container with <code>--rm</code>, because you may want to inspect the filesystem after exiting, but most of the times it&#39;s an oversight.</li>
<li>Dangling images. Whenever you build a brand-new Docker image with the same tag as an existing one, you&#39;ll be possibly left with an old, dangling image – i.e an image you can only use by tag, not by name. The same could happen when an upstream image changes. More or less just like a <a href="https://git-scm.com/book/it/v2/Git-Internals-Maintenance-and-Data-Recovery" rel="nofollow">git dangling object</a>. <em>Small note: it doesn&#39;t necessarily happen. The new image can be a perfect child of the previous image, in that case Docker uses a kind of inheritance mechanism which produces no dangling images. But if the image changes “in the middle” of the Dockerfile, and hence a piece of the old image get invalidated, then you&#39;ll be left with some dangling images</em>.</li></ul>

<p>So, here&#39;s the script. Just put it in your <code>/etc/cron.daily</code> directory and live quite happily ever after:</p>

<pre><code>#!/bin/bash
docker ps -a -q --filter=&#34;status=created&#34; | xargs --no-run-if-empty \
docker rm
docker ps -a -q --filter=&#34;status=exited&#34; | xargs --no-run-if-empty \
docker rm
docker images -q -f dangling=true | xargs --no-run-if-empty \
docker rmi
</code></pre>

<p><a href="/alan/tag:MostlyUnixish" class="hashtag" rel="nofollow"><span>#</span><span class="p-category">MostlyUnixish</span></a> <a href="/alan/tag:docker" class="hashtag" rel="nofollow"><span>#</span><span class="p-category">docker</span></a></p>
]]></content:encoded>
      <guid>https://www.franzoni.eu/alan/cleaning-up-docker-leavings</guid>
      <pubDate>Tue, 13 Sep 2016 07:28:33 +0000</pubDate>
    </item>
  </channel>
</rss>