{"id":491444,"date":"2022-09-09T03:48:26","date_gmt":"2022-09-09T00:48:26","guid":{"rendered":"https:\/\/en.buradabiliyorum.com\/how-to-make-docker-rebuild-an-image-without-its-cache\/"},"modified":"2022-09-09T03:48:26","modified_gmt":"2022-09-09T00:48:26","slug":"how-to-make-docker-rebuild-an-image-without-its-cache","status":"publish","type":"post","link":"https:\/\/buradabiliyorum.com\/en\/how-to-make-docker-rebuild-an-image-without-its-cache\/","title":{"rendered":"#How to Make Docker Rebuild an Image Without Its Cache"},"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-6a3a1c0a69c81\" 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-6a3a1c0a69c81\" 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-1'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-make-docker-rebuild-an-image-without-its-cache\/#%E2%80%9CHow_to_Make_Docker_Rebuild_an_Image_Without_Its_Cache%E2%80%9D\" >&#8220;How to Make Docker Rebuild an Image Without Its Cache&#8221;<\/a><ul class='ez-toc-list-level-2' ><li class='ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-make-docker-rebuild-an-image-without-its-cache\/#How_the_Cache_Works\" >How the Cache Works<\/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-make-docker-rebuild-an-image-without-its-cache\/#Disabling_the_Cache\" >Disabling the Cache<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-make-docker-rebuild-an-image-without-its-cache\/#Pulling_Updated_Base_Images\" >Pulling Updated Base Images<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-make-docker-rebuild-an-image-without-its-cache\/#Using_Docker_Compose\" >Using Docker Compose<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-make-docker-rebuild-an-image-without-its-cache\/#Cleaning_the_Build_Cache\" >Cleaning the Build Cache<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-make-docker-rebuild-an-image-without-its-cache\/#Summary\" >Summary<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n<h1><span class=\"ez-toc-section\" id=\"%E2%80%9CHow_to_Make_Docker_Rebuild_an_Image_Without_Its_Cache%E2%80%9D\"><\/span>&#8220;How to Make Docker Rebuild an Image Without Its Cache&#8221;<span class=\"ez-toc-section-end\"><\/span><\/h1>\n<div>\n<img loading=\"lazy\" decoding=\"async\" class=\"type:primaryImage alignnone size-full wp-image-805981\" data-pagespeed-no-defer=\"\" src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2022\/05\/Docker-New.jpeg?width=1198&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Graphic showing the Docker logo\" width=\"1600\" height=\"900\"\/><\/p>\n<p>Docker caches image build results to accelerate subsequent rebuilds. While this mechanism is <a href=\"https:\/\/buradabiliyorum.com\/en\/category\/general\/\" data-internallinksmanager029f6b8e52c=\"3\" title=\"General\" target=\"_blank\" rel=\"noopener\">general<\/a>ly reliable, sometimes you\u2019ll want to rebuild an image without using the cache. This could be to diagnose issues or check the complete build procedure will be reproducible in a clean environment.<\/p>\n<p>In this article, you\u2019ll learn how to achieve a fresh build without manually deleting the cache. You\u2019ll also see how to pull updated base images so your build matches the output that a new Docker installation would produce.<\/p>\n<h2 id=\"how-the-cache-works\"><span class=\"ez-toc-section\" id=\"How_the_Cache_Works\"><\/span>How the Cache Works<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Here\u2019s a simple Dockerfile:<\/p>\n<pre>FROM alpine:latest&#13;\nCOPY 1.txt \/1.txt&#13;\nCOPY 2.txt \/2.txt<\/pre>\n<p>Populate the sample files in your working directory and build the image:<\/p>\n<pre>$ echo 1 &gt; 1.txt&#13;\n$ echo 2 &gt; 2.txt&#13;\n$ docker build -t demo:latest .<\/pre>\n<p>The output will look similar to this:<\/p>\n<pre>Sending build context to Docker daemon   5.12kB&#13;\nStep 1\/3 : FROM alpine:latest&#13;\n ---&gt; 9c6f07244728&#13;\nStep 2\/3 : COPY 1.txt \/1.txt&#13;\n ---&gt; db61ff73c0b5&#13;\nStep 3\/3 : COPY 2.txt \/2.txt&#13;\n ---&gt; f1129e47fc12&#13;\nSuccessfully built f1129e47fc12&#13;\nSuccessfully tagged demo:latest<\/pre>\n<p>Now modify <code>2.txt<\/code> and then rebuild the image:<\/p>\n<pre>$ echo two &gt; 2.txt&#13;\n$ docker build -t demo:latest .&#13;\nSending build context to Docker daemon   5.12kB&#13;\nStep 1\/3 : FROM alpine:latest&#13;\n ---&gt; 9c6f07244728&#13;\nStep 2\/3 : COPY 1.txt \/1.txt&#13;\n ---&gt; Using cache&#13;\n ---&gt; db61ff73c0b5&#13;\nStep 3\/3 : COPY 2.txt \/2.txt&#13;\n ---&gt; 75ba7d786049&#13;\nSuccessfully built 75ba7d786049&#13;\nSuccessfully tagged demo:latest<\/pre>\n<p>The second build stage shows <code>Using cache<\/code> because the contents of <code>1.txt<\/code> haven\u2019t changed. The inter<a href=\"https:\/\/buradabiliyorum.com\/en\/category\/social-mediaa\/\" data-internallinksmanager029f6b8e52c=\"1\" title=\"Social Media\" target=\"_blank\" rel=\"noopener\">media<\/a>te layer with ID <code>db61ff73c0b5<\/code> gets reused for the new build.<\/p>\n<h2 id=\"disabling-the-cache\"><span class=\"ez-toc-section\" id=\"Disabling_the_Cache\"><\/span>Disabling the Cache<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>You can disable use of the intermediate layer cache by including the <code>--no-cache<\/code> flag when you run <code>docker build<\/code>:<\/p>\n<pre>$ echo second &gt; 2.txt&#13;\n$ docker build --no-cache -t demo:latest .&#13;\nSending build context to Docker daemon   5.12kB&#13;\nStep 1\/3 : FROM alpine:latest&#13;\n ---&gt; 9c6f07244728&#13;\nStep 2\/3 : COPY 1.txt \/1.txt&#13;\n ---&gt; 1590b2407dd4&#13;\nStep 3\/3 : COPY 2.txt \/2.txt&#13;\n ---&gt; afb31630ce32&#13;\nSuccessfully built afb31630ce32&#13;\nSuccessfully tagged demo:latest<\/pre>\n<p>This time a new image layer, ID <code>1590b2407dd4<\/code>, has been created by the second build stage, even though <code>1.txt<\/code> remains unmodified.<\/p>\n<p>You can use the <code>--no-cache<\/code> flag to force a rebuild when you suspect the cache is stale or you want to start from scratch. Build performance will be reduced because Docker will need to recreate every layer.<\/p>\n<h2 id=\"pulling-updated-base-images\"><span class=\"ez-toc-section\" id=\"Pulling_Updated_Base_Images\"><\/span>Pulling Updated Base Images<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Another form of caching concerns the base images which your Dockerfile references in its <code>FROM<\/code> instructions. The example image above uses <code>alpine:latest<\/code> as its base. Docker won\u2019t automatically pull an updated version of <code>alpine:latest<\/code>, unless the image doesn\u2019t already exist on your host.<\/p>\n<p>This means the first build of a Dockerfile will pull down the base images you need, as long as you don\u2019t already have them. Subsequent rebuilds won\u2019t refresh the images so you could be building on an outdated base. Building the image on a different Docker host might not produce the same output if that host pulls a newer version of the base.<\/p>\n<p>You can force Docker to check for updated base images at build time by adding the <code>--pull<\/code> flag to your <code>docker build<\/code> command. This is separate to <code>--no-cache<\/code>. Using <code>--pull<\/code> will retrieve the image tag\u2019s manifest from its registry and compare it to the version on your machine. The new image will be pulled from the registry when there\u2019s a discrepancy in the manifests.<\/p>\n<pre>$ docker build --no-cache --pull -t demo:latest .&#13;\nSending build context to Docker daemon   5.12kB&#13;\nStep 1\/3 : FROM alpine:latest&#13;\nlatest: Pulling from library\/alpine&#13;\nDigest: sha256:bc41182d7ef5ffc53a40b044e725193bc10142a1243f395ee852a8d9730fc2ad&#13;\nStatus: Image is up to date for alpine:latest&#13;\n ---&gt; 9c6f07244728&#13;\nStep 2\/3 : COPY 1.txt \/1.txt&#13;\n ---&gt; 4fee970dfaab&#13;\nStep 3\/3 : COPY 2.txt \/2.txt&#13;\n ---&gt; 60d2e3fff0fb&#13;\nSuccessfully built 60d2e3fff0fb&#13;\nSuccessfully tagged demo:latest<\/pre>\n<p>The above example shows how the build output changes when the <code>--pull<\/code> flag is used. Docker tries to pull the Alpine base image, even though it\u2019s already on the machine from the earlier example builds. The image digest (SHA) is printed to the terminal. In this case the digest is unchanged from the local version so Docker reports that the image is already up to date.<\/p>\n<p>The <code>--no-cache<\/code> and <code>--pull<\/code> flags can be used independently or in combination. Adding them both gives you the cleanest slate for starting your build, forcing Docker to check for updated base images and disregard any previously created layers. It\u2019s equivalent to running the build on a newly installed Docker host.<\/p>\n<h2 id=\"using-docker-compose\"><span class=\"ez-toc-section\" id=\"Using_Docker_Compose\"><\/span>Using Docker Compose<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Docker Compose supports both the <code>--no-cache<\/code> and <code>--pull<\/code> flags too. They have the same effect as their <code>docker build<\/code> counterparts.<\/p>\n<pre># Compose v2&#13;\n$ docker compose build --no-cache --pull&#13;\n&#13;\n# Compose v1&#13;\n$ docker-compose build --no-cache --pull<\/pre>\n<h2 id=\"cleaning-the-build-cache\"><span class=\"ez-toc-section\" id=\"Cleaning_the_Build_Cache\"><\/span>Cleaning the Build Cache<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>You can clean the build cache to guarantee it\u2019s disused. This also helps to free up excess disk space consumed by cached build layers.<\/p>\n<p>Run the <code>docker builder prune<\/code> command to empty your cache. It only works when you\u2019re building images with the modern BuildKit build engine.<\/p>\n<pre>$ docker builder prune<\/pre>\n<p>Running the command without arguments only removes the dangling build cache. This relates to caches that relate to images no longer present on your host. Add the <code>-a<\/code> flag to completely empty the cache, including layers that are used by images:<\/p>\n<pre>$ docker builder prune -a<\/pre>\n<p>Pruning also accepts a <code>--filter<\/code> flag that can be used to target caches modified before or since a particular time period.<\/p>\n<pre># Delete caches modified in the past two hours&#13;\n$ docker build prune --filter since=2h&#13;\n&#13;\n# Delete caches modified more than two hours ago&#13;\n$ docker build prune --filter until=2h<\/pre>\n<h2 id=\"summary\"><span class=\"ez-toc-section\" id=\"Summary\"><\/span>Summary<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The Docker build cache improves performance by reusing intermediate image layers between builds. This avoids wasted work to recreate layers that already exist and haven\u2019t changed. While the build cache is generally desirable, there are scenarios where you might want to run a build without it. Adding the <code>--no-cache<\/code> flag to your builds provides an accurate perspective of what the build would produce when run on a new host.<\/p>\n<p>Stale base images need to be considered alongside the build cache too. Docker reuses local versions of base images by default, which can cause you to build new images on an outdated base. Using the <code>--pull<\/code> flag forces Docker to check for updated base images before it starts the build, giving you greater consistency across different environments.<\/p>\n<\/div>\n<p><script>\n setTimeout(function(){\n  !function(f,b,e,v,n,t,s)\n  {if(f.fbq)return;n=f.fbq=function(){n.callMethod?\n  n.callMethod.apply(n,arguments):n.queue.push(arguments)};\n  if(!f._fbq)f._fbq=n;n.push=n;n.loaded=!0;n.version='2.0';\n  n.queue=[];t=b.createElement(e);t.async=!0;\n  t.src=v;s=b.getElementsByTagName(e)[0];\n  s.parentNode.insertBefore(t,s) } (window, document,'script',\n  'https:\/\/connect.facebook.net\/en_US\/fbevents.js');\n   fbq('init', '335401813750447');\n   fbq('track', 'PageView');\n  },3000);\n<\/script><\/p>\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.howtogeek.com\/devops\/how-to-make-docker-rebuild-an-image-without-its-cache\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;How to Make Docker Rebuild an Image Without Its Cache&#8221; Docker caches image build results to accelerate subsequent rebuilds. While this mechanism is generally reliable, sometimes you\u2019ll want to rebuild an image without using the cache. This could be to diagnose issues or check the complete build procedure will be reproducible in a clean environment&#8230;.<\/p>\n","protected":false},"author":1,"featured_media":491445,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"fifu_image_url":"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2022\/05\/Docker-New.jpeg?height=200p&trim=2,2,2,2","fifu_image_alt":"","footnotes":""},"categories":[18],"tags":[],"class_list":["post-491444","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\/491444","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=491444"}],"version-history":[{"count":0,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/491444\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media\/491445"}],"wp:attachment":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media?parent=491444"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/categories?post=491444"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/tags?post=491444"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}