<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Jacob Gillespie</title>
    <description>The latest articles on Forem by Jacob Gillespie (@jacobwgillespie).</description>
    <link>https://forem.com/jacobwgillespie</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F873923%2F65913d9d-7f8d-41e4-82dd-43510260f01a.jpeg</url>
      <title>Forem: Jacob Gillespie</title>
      <link>https://forem.com/jacobwgillespie</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/jacobwgillespie"/>
    <language>en</language>
    <item>
      <title>Fast Dockerfiles: theory and practice</title>
      <dc:creator>Jacob Gillespie</dc:creator>
      <pubDate>Tue, 07 Jun 2022 22:31:37 +0000</pubDate>
      <link>https://forem.com/depot/fast-dockerfiles-theory-and-practice-2d08</link>
      <guid>https://forem.com/depot/fast-dockerfiles-theory-and-practice-2d08</guid>
      <description>&lt;p&gt;The core theory behind fast Docker builds is conceptually straightforward: write your Dockerfile such that as many possible build steps can be reused between builds. By skipping build steps entirely, the build has less to do and completes faster!&lt;/p&gt;

&lt;h2&gt;
  
  
  An example with Node.js
&lt;/h2&gt;

&lt;p&gt;To illustrate Docker layer caching, let's take a look at an example Node.js application (the same principles apply to most all Dockerfiles though).&lt;/p&gt;

&lt;p&gt;Say you have a Node.js application, and you want to package it into a container. Understanding the basic Dockerfile building blocks of &lt;code&gt;FROM&lt;/code&gt;, &lt;code&gt;RUN&lt;/code&gt;, &lt;code&gt;COPY&lt;/code&gt;, etc, you might start with:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight docker"&gt;&lt;code&gt;&lt;span class="k"&gt;FROM&lt;/span&gt;&lt;span class="s"&gt; node:16&lt;/span&gt;

&lt;span class="k"&gt;WORKDIR&lt;/span&gt;&lt;span class="s"&gt; /app&lt;/span&gt;
&lt;span class="k"&gt;COPY&lt;/span&gt;&lt;span class="s"&gt; . .&lt;/span&gt;
&lt;span class="k"&gt;RUN &lt;/span&gt;npm &lt;span class="nb"&gt;install&lt;/span&gt;
&lt;span class="k"&gt;RUN &lt;/span&gt;npm build
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With those building blocks you're all set, you have a working container image!&lt;/p&gt;

&lt;p&gt;This is not the fastest Dockerfile. The way Docker layer caching works is by reusing steps ("layers") from the previous build that have not changed. When it encounters a layer that has changed, it needs to rebuild that layer and all following layers.&lt;/p&gt;

&lt;p&gt;With that in mind, you may have noticed an issue with our initial Dockerfile. By placing the &lt;code&gt;COPY . .&lt;/code&gt; step that copies source code into the container before anything else, any time that source code changes all the following steps must re-run.&lt;/p&gt;

&lt;p&gt;But we know that the &lt;code&gt;npm install&lt;/code&gt; step only really depends on our &lt;code&gt;package.json&lt;/code&gt; and &lt;code&gt;package-lock.json&lt;/code&gt; files (in this example at least). However, currently, when &lt;em&gt;any&lt;/em&gt; file in our project changes, the &lt;code&gt;npm install&lt;/code&gt; step must re-run, which can be slow. So an iterative improvement would be to only copy the dependent files into the image first, then run &lt;code&gt;npm install&lt;/code&gt;, then copy the rest of the files:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight docker"&gt;&lt;code&gt;&lt;span class="k"&gt;FROM&lt;/span&gt;&lt;span class="s"&gt; node:16&lt;/span&gt;

&lt;span class="k"&gt;WORKDIR&lt;/span&gt;&lt;span class="s"&gt; /app&lt;/span&gt;
&lt;span class="k"&gt;COPY&lt;/span&gt;&lt;span class="s"&gt; package.json package-lock.json /app/&lt;/span&gt;
&lt;span class="k"&gt;RUN &lt;/span&gt;npm &lt;span class="nb"&gt;install&lt;/span&gt;
&lt;span class="k"&gt;COPY&lt;/span&gt;&lt;span class="s"&gt; . .&lt;/span&gt;
&lt;span class="k"&gt;RUN &lt;/span&gt;npm build
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we have improved the cache hits and reduced the build time! If files change, but the &lt;code&gt;package.json&lt;/code&gt; and &lt;code&gt;package-lock.json&lt;/code&gt; files don't, the &lt;code&gt;npm install&lt;/code&gt; step is skipped.&lt;/p&gt;

&lt;p&gt;What about the &lt;code&gt;npm build&lt;/code&gt; step?&lt;/p&gt;

&lt;p&gt;That's a bit more complicated, that builds your entire application, and it's not straightforward to only copy the files that it uses as that effectively means listing almost all files in your project.&lt;/p&gt;

&lt;p&gt;But at the same time, there are clearly files that &lt;em&gt;aren't&lt;/em&gt; needed for the build. Like perhaps the &lt;code&gt;README.md&lt;/code&gt; file or editor configuration. Ideally, those files would not unnecessarily invalidate the build cache.&lt;/p&gt;

&lt;p&gt;For that, we can use a &lt;code&gt;.dockerignore&lt;/code&gt; file - this is a file placed at the root of your build context that describes files that Docker should skip copying into the build context, meaning they won't be considered as part of the build, won't be copied into the container, and won't be considered when checking build cache.&lt;/p&gt;

&lt;p&gt;For our Node.js project, we might create a &lt;code&gt;.dockerignore&lt;/code&gt; file like so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;dist
node_modules
README.md
.editorconfig
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That excludes the &lt;code&gt;dist&lt;/code&gt; directory, the &lt;code&gt;node_modules&lt;/code&gt; directory, the &lt;code&gt;README.md&lt;/code&gt; file, and the &lt;code&gt;.editorconfig&lt;/code&gt; file, so none of them will be copied into the container and none of them can invalidate the previous build layers if they change.&lt;/p&gt;

&lt;h2&gt;
  
  
  Applying the theory
&lt;/h2&gt;

&lt;p&gt;While the theory is simple, order build steps to maximize reusing previous layers, in practice it can be tricky to get working and maintain the dependencies between project files and &lt;code&gt;RUN&lt;/code&gt; steps as project structures evolve. But by spending some time ordering the build steps and crafting &lt;code&gt;.dockerignore&lt;/code&gt; files, you can get major speedups in your Docker builds!&lt;/p&gt;

&lt;p&gt;Docker and Docker's modern build engine BuildKit also provide some advanced features that allow you to even further optimize using caches, including things like mounting directories directly into the build so that builds can be incremental. We will explore some of these options in a following post.&lt;/p&gt;

&lt;h2&gt;
  
  
  Building images with Depot
&lt;/h2&gt;

&lt;p&gt;Depot provides hosted Docker builder instances - a remote server that runs Docker builds.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;All the Docker caching theory and techniques work without Depot&lt;/strong&gt;. You can get all of the performance improvements discussed here using just your local machine. Many of the simple and advanced optimizations work in CI providers too (though with some limitations).&lt;/p&gt;

&lt;p&gt;But Depot is designed specifically to work with and enhance the Docker layer cache, adding a few additional features and speedups:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;In CI, Depot is often several times faster as it natively saves and loads your previous build cache on local SSDs. Rather than needing to download the previous build's cache from remote storage, instead it's instantly available for the next build to use.&lt;/li&gt;
&lt;li&gt;Because Depot offers remote builders, the Docker layer cache is shared among everyone with access to the project, so if your coworker builds the same project, they can reuse the same cache. You might be able to skip &lt;em&gt;all&lt;/em&gt; of the build steps, the theoretical maximum speedup!&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;If this sounds interesting, we'd love for you to &lt;a href="https://depot.dev/sign-up" rel="noopener noreferrer"&gt;give Depot a try&lt;/a&gt;, and run some Docker builds using your local machine or existing CI provider.&lt;/p&gt;

</description>
      <category>docker</category>
      <category>devops</category>
    </item>
  </channel>
</rss>
