<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Docker on Wimpy's World</title><link>https://blog.wimpys.world/tags/docker/</link><description>Recent content in Docker on Wimpy's World</description><generator>Hugo -- gohugo.io</generator><language>en-gb</language><managingEditor>martin@wimpress.com (Martin Wimpress)</managingEditor><webMaster>martin@wimpress.com (Martin Wimpress)</webMaster><lastBuildDate>Fri, 18 Jun 2021 12:18:15 +0000</lastBuildDate><atom:link href="https://blog.wimpys.world/tags/docker/rss.xml" rel="self" type="application/rss+xml"/><item><title>Creating Production-Ready Containers - Advanced Techniques</title><link>https://blog.wimpys.world/posts/creating-production-ready-containers-advanced-techniques/</link><pubDate>Fri, 18 Jun 2021 12:18:15 +0000</pubDate><author>martin@wimpress.com (Martin Wimpress)</author><guid>https://wimpysworld.com/posts/creating-production-ready-containers-advanced-techniques/</guid><description>&lt;p&gt;Creating production-ready containers for use in commercial-grade apps can be a far cry from the &amp;ldquo;get started with Node.js and Docker&amp;rdquo;-type of tutorials that are common around the Internet. Those guides are great for introducing all the advantages of Docker containers in modern cloud-native development, but creating a container that passes muster in a large-scale application in production is a different story.&lt;/p&gt;
&lt;p&gt;For production-ready containers, there are three key things you want to optimise for when creating a container:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Image Size 📦&lt;/li&gt;
&lt;li&gt;Build Speed 🐢&lt;/li&gt;
&lt;li&gt;Security 🔐&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Image size and build speed ensure that your containers can move through CI/CD and test pipelines easily and efficiently. Security is critical in today&amp;rsquo;s software supply chain, and containers have their own set of security issues. Thankfully, reducing container image size actually can alleviate some security issues in containers.&lt;/p&gt;
&lt;p&gt;&lt;a href="https://blog.wimpys.world/posts/creating-production-ready-containers-the-basics"&gt;In my Basics article&lt;/a&gt;, I showed you some easy techniques to improve your &lt;code&gt;Dockerfile&lt;/code&gt; using a sample &amp;ldquo;Hello World&amp;rdquo; Node.js application.&lt;/p&gt;
&lt;p&gt;These basics address all three optimisations, though they only scratch the surface.&lt;/p&gt;
&lt;p&gt;Let&amp;rsquo;s look at some more advanced techniques for Container Optimisation.&lt;/p&gt;
&lt;h2 id="alpine-images"&gt;Alpine Images&lt;/h2&gt;
&lt;p&gt;The very first thing you&amp;rsquo;ll encounter when looking for techniques to create smaller containers is &lt;a href="https://alpinelinux.org/"&gt;Alpine Linux&lt;/a&gt;. Alpine Linux is an open-source project whose goal is to create a bare-bones 🦴 version of Linux that lets developers &amp;ldquo;build from the ground up.&amp;rdquo;&lt;/p&gt;
&lt;h3 id="pros-transitioning-to-alpine-can-be-an-easy-way-to-get-a-smaller-container"&gt;Pros: Transitioning to Alpine can be an easy way to get a smaller container&lt;/h3&gt;
&lt;p&gt;Reducing image size with Alpine can be incredibly simple - under the right circumstances. For some apps, it&amp;rsquo;s as easy as changing the base image in your &lt;code&gt;Dockerfile&lt;/code&gt;:&lt;/p&gt;
&lt;h4 id="from"&gt;FROM&lt;/h4&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-dockerfile" data-lang="dockerfile"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;FROM&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;node:16.2.0&lt;/span&gt;&lt;span class="err"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h4 id="to"&gt;TO&lt;/h4&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-dockerfile" data-lang="dockerfile"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;FROM&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;node:16.2.0-alpine&lt;/span&gt;&lt;span class="err"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;When we build the new image, we see that the old image was 856MB and the new one is 114MB 🎉&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-text" data-lang="text"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;REPOSITORY TAG IMAGE ID CREATED SIZE
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;cotw-node-alpine latest 2cc7b4a7b09c 2 minutes ago 114MB
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;cotw-node latest 873fb9fca53a 3 days ago 856MB
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Easy, right? Not so fast.&lt;/p&gt;
&lt;h3 id="cons-using-alpine-images-can-lead-to-build-problems-now-and-in-the-future"&gt;Cons: Using Alpine images can lead to build problems, now and in the future&lt;/h3&gt;
&lt;p&gt;There are some not-so-obvious gotchas with using Alpine images that don&amp;rsquo;t crop up in our super simple example application, such as:&lt;/p&gt;
&lt;h4 id="you-have-to-install-everything"&gt;You have to install everything&lt;/h4&gt;
&lt;p&gt;Those tiny base images have to sacrifice something, right? Alpine users will be installing everything they need, right down to time-zone data or development tools. You won&amp;rsquo;t need your development tools for your production image, most likely, but for most developers, the thought of a server without &lt;code&gt;curl&lt;/code&gt; or &lt;code&gt;vim&lt;/code&gt; is a bridge too far.&lt;/p&gt;
&lt;h4 id="different-compilers-and-package-managers"&gt;Different compilers and package managers&lt;/h4&gt;
&lt;p&gt;You&amp;rsquo;ll also be installing any dependencies with the Alpine Package Keeper tool (&lt;code&gt;apk&lt;/code&gt;) instead of the more familiar &lt;code&gt;apt&lt;/code&gt; or &lt;code&gt;rpm&lt;/code&gt;. The differences are small but can trip up unsuspecting developers.&lt;/p&gt;
&lt;h4 id="fewer-examples-less-documentation"&gt;Fewer examples; less documentation&lt;/h4&gt;
&lt;p&gt;Finally, while Alpine has been around for nine-plus years, it is and likely always will be a smaller and more specialised user base than established Linux distributions such as Ubuntu and Debian. To wit, at the time of this writing the &lt;code&gt;alpine&lt;/code&gt; tag on StackOverflow has just &lt;a href="https://stackoverflow.com/questions/tagged/alpine"&gt;1,280 questions&lt;/a&gt;, compared with &lt;a href="https://stackoverflow.com/questions/tagged/ubuntu"&gt;over 54,000 for Ubuntu&lt;/a&gt;.&lt;/p&gt;
&lt;h2 id="multi-stage-builds"&gt;Multi-stage builds&lt;/h2&gt;
&lt;p&gt;The next tactic you are likely to encounter when searching for ways to reduce Docker image sizes is multi-stage 🏗 builds. This tactic, &lt;a href="https://docs.docker.com/develop/develop-images/multistage-build/"&gt;recommended by Docker and many in the Docker community&lt;/a&gt;, is essentially building the image twice. The first set of commands builds your base application image, all things included. The second set of commands builds an image off of that base image, taking only what&amp;rsquo;s needed and leaving out anything that&amp;rsquo;s not.&lt;/p&gt;
&lt;p&gt;With a multi-stage build, our &lt;code&gt;Dockerfile&lt;/code&gt; would look like this. Notice the two &lt;code&gt;FROM&lt;/code&gt; statements. The first builds the application image; the second copies the necessary files from that image into the second, more production-ready version.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-dockerfile" data-lang="dockerfile"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;FROM&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;node:16.2.0-alpine&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;as&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;builder&lt;/span&gt;&lt;span class="err"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;WORKDIR&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;/usr/src/app&lt;/span&gt;&lt;span class="err"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;COPY&lt;/span&gt; package*.json ./&lt;span class="err"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;RUN&lt;/span&gt; npm ci&lt;span class="err"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;COPY&lt;/span&gt; app.js ./&lt;span class="err"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="err"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;FROM&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;node:16.2.0-alpine&lt;/span&gt;&lt;span class="err"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;WORKDIR&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;/usr/src/app&lt;/span&gt;&lt;span class="err"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;COPY&lt;/span&gt; --from&lt;span class="o"&gt;=&lt;/span&gt;builder /usr/src/app .&lt;span class="err"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;EXPOSE&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;3000&lt;/span&gt;&lt;span class="err"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;USER&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;node&lt;/span&gt;&lt;span class="err"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;CMD&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;&amp;#34;node&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s2"&gt;&amp;#34;app.js&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="err"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h3 id="pros-dev-and-prod-images-can-be-built-separately"&gt;Pros: Dev and Prod images can be built separately&lt;/h3&gt;
&lt;p&gt;When combined with &lt;a href="https://docs.docker.com/compose/"&gt;Docker Compose&lt;/a&gt;, this approach gives developers a flexible development environment while reducing bloat in the production images. You can simply use your initial image for dev/test and the final version for production. Multi-stage builds work especially well for Go containers, significantly reducing image size, but also work well for static Node.js and React-type applications.&lt;/p&gt;
&lt;h3 id="cons-added-complexity-use-case-specific"&gt;Cons: Added complexity; use-case specific&lt;/h3&gt;
&lt;p&gt;Multi-stage builds are still relatively new 🌱 on the scene. For most developers still new to containers, knowing what to copy over to the final production image and what to leave behind is a major barrier to entry. Further, this pattern can run into challenges.&lt;/p&gt;
&lt;p&gt;Since we&amp;rsquo;re already using an Alpine image, the size savings are relatively minor for our &amp;ldquo;Hello World&amp;rdquo; example. You&amp;rsquo;d expect to see greater gains in a full-blown React or Vue application.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-text" data-lang="text"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;REPOSITORY TAG IMAGE ID CREATED SIZE
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;cotw-node-multistage latest 52bc33d14a87 3 minutes ago 114MB
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;cotw-node-alpine latest 2cc7b4a7b09c 4 days ago 114MB
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;cotw-node latest 873fb9fca53a 7 days ago 856MB
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id="development-tools-and-distroless"&gt;Development tools and Distroless&lt;/h2&gt;
&lt;p&gt;There are several tools - and new ones emerging every day - that look to bypass or automate &lt;code&gt;Dockerfile&lt;/code&gt; authoring to make image creation easier. &lt;a href="https://buildpacks.io/"&gt;&lt;em&gt;Buildpacks&lt;/em&gt;&lt;/a&gt; are the most mature of these technologies and can be used through tools like &lt;a href="https://buildpacks.io/docs/tools/pack/"&gt;Pack&lt;/a&gt; or &lt;a href="https://www.waypointproject.io/plugins/pack"&gt;Waypoint&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;There are builder options from multiple sources - Heroku, Google, and Paketo are common favourites - and each gives you a slightly different developer experience and final image when used.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;$ pack build cotw-node-bp-google --builder gcr.io/buildpacks/builder:v1
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;$ pack build cotw-node-bp-heroku --builder heroku/buildpacks:18
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;$ pack build cotw-node-bp-pb-base --builder paketobuildpacks/builder:base
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;$ pack build cotw-node-bp-pb-full --builder paketobuildpacks/builder:full
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h3 id="pros-when-they-work-they-work"&gt;Pros: When they work, they work&lt;/h3&gt;
&lt;p&gt;In certain instances, Buildpacks can take the pain out of &lt;code&gt;Dockerfile&lt;/code&gt; authoring and just create container images of your application with no fuss. The pack tool is looking for &amp;ldquo;app-like&amp;rdquo; files in your source directory, and automatically figuring out what kind of application is there and how to containerize it. In the case of our Node sample, it sees &lt;code&gt;package.json&lt;/code&gt; and correctly assumes we have a Node.js application.&lt;/p&gt;
&lt;h3 id="cons-when-they-dont"&gt;Cons: When they don&amp;rsquo;t…&lt;/h3&gt;
&lt;p&gt;Given the relative newness of this approach for Docker containers, there are a lot of gotchas with Buildpacks. Non-standard applications or operating systems can struggle, and we&amp;rsquo;ve had issues running them successfully on the new Silicon Macbook Pros. The resulting images vary a lot - we saw a range of 200MB to 800MB in our examples - and the results tend to be lower than what you&amp;rsquo;d get with other techniques.&lt;/p&gt;
&lt;h2 id="automate-it-with-slimtoolkit"&gt;Automate it with SlimToolKit&lt;/h2&gt;
&lt;p&gt;The &lt;a href="https://slimtoolkit.org/"&gt;SlimToolKit&lt;/a&gt; (&lt;em&gt;formerly DockerSlim&lt;/em&gt;) open-source project was created by &lt;a href="https://slim.ai"&gt;Slim.AI&lt;/a&gt; CTO &lt;a href="https://twitter.com/kcqon"&gt;Kyle Quest&lt;/a&gt; in 2015 as a way to automate container optimisation. Simply download and run &lt;code&gt;slim build &amp;lt;myimage&amp;gt;&lt;/code&gt; and SlimToolKit will examine the image, rebuild it with only the required dependencies, and give you a new image that can be run just like the original.&lt;/p&gt;
&lt;h3 id="pros-its-automatic"&gt;Pros: It&amp;rsquo;s automatic&lt;/h3&gt;
&lt;p&gt;SlimToolKit means you can work with whatever base image you&amp;rsquo;d like (say, Ubuntu or Debian) and let SlimToolKit worry about removing unnecessary tools and files en route to production. The best part is that SlimToolKit can be used alongside any of these other techniques. Once tested, it can be integrated into your CI/CD pipeline for automatic container minification, and the reduction in size leads to faster build times and better security.&lt;/p&gt;
&lt;h3 id="cons-steep-learning-curve"&gt;Cons: Steep learning curve&lt;/h3&gt;
&lt;p&gt;As with any open-source software, SlimToolKit can take some time to get working, especially for non-trivial applications. It works best for web-style applications, micro-services and APIs that have defined HTTP/HTTPS ports which the sensor can find and use to observe the container internals.&lt;/p&gt;
&lt;p&gt;For best results, spend some time getting to know the various command flags available to tune your image, and &lt;a href="https://github.com/slimtoolkit/examples"&gt;take a look at the examples for whatever framework you&amp;rsquo;re using&lt;/a&gt;.&lt;/p&gt;
&lt;h2 id="connecting-with-slimtoolkit"&gt;Connecting with SlimToolKit&lt;/h2&gt;
&lt;p&gt;There&amp;rsquo;s an &lt;a href="https://discord.gg/uBttmfyYNB"&gt;active Slim.AI Discord channel&lt;/a&gt; full of experts who can help you triage issues as they arise.&lt;/p&gt;</description><summary>Advanced techniques for production-ready container best practice</summary></item><item><title>Creating Production-Ready Containers - The Basics</title><link>https://blog.wimpys.world/posts/creating-production-ready-containers-the-basics/</link><pubDate>Thu, 03 Jun 2021 12:18:15 +0000</pubDate><author>martin@wimpress.com (Martin Wimpress)</author><guid>https://wimpysworld.com/posts/creating-production-ready-containers-the-basics/</guid><description>&lt;p&gt;So you&amp;rsquo;ve coded an awesome app and you are ready to deploy it to the cloud. You&amp;rsquo;ve heard a lot about &lt;a href="https://www.docker.com/"&gt;Docker&lt;/a&gt; and completed a few online tutorials to containerise your app. All set, right? But what do you need to know if you&amp;rsquo;re going to move that app to a production environment on the public Internet? What if you&amp;rsquo;re using it for your job and need to pass security scans and DevOps checks?&lt;/p&gt;
&lt;p&gt;In this series, I introduce some basic concepts for making production-ready containers. I also introduce the concept of &amp;ldquo;slimming&amp;rdquo; a container. &lt;strong&gt;Slimming&lt;/strong&gt; refers to both optimising and minifying your Docker containers, reducing them in size by up to 80-percent while also making them more secure by decreasing the attack surface. Slimming your container is also a great way to implement &lt;a href="https://www.slim.ai/blog/why-dont-we-practice-container-best-practices"&gt;container best practices&lt;/a&gt; without re-engineering your entire workflow.&lt;/p&gt;
&lt;p&gt;There are many ways to slim a container, from basic security to fully automated open-source tools like &lt;a href="https://slimtoolkit.org/"&gt;SlimToolKit&lt;/a&gt; (formerly &lt;a href="https://dockersl.im/"&gt;DockerSlim&lt;/a&gt;). &lt;em&gt;Full disclosure&lt;/em&gt;: I used work for &lt;a href="https://slim.ai"&gt;Slim.AI&lt;/a&gt;, a company founded on the SlimToolKit open source project. Let&amp;rsquo;s look at some of the common ways developers create production-ready container images today.&lt;/p&gt;
&lt;p&gt;I&amp;rsquo;ll explore each of these in a separate article using a simple &amp;ldquo;Hello World&amp;rdquo; Node.js example that can be found in many online tutorials.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-js" data-lang="js"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kr"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;express&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;express&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kr"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;express&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kr"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;port&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;3000&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;/&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;Hello World!&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;})&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;listen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;port&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sb"&gt;`Example app listening at http://localhost:&lt;/span&gt;&lt;span class="si"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;port&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sb"&gt;`&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;})&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Let&amp;rsquo;s get started by simply improving your &lt;code&gt;Dockerfile&lt;/code&gt; to build a better Docker image.&lt;/p&gt;
&lt;h2 id="creating-a-better-dockerfile"&gt;Creating a Better Dockerfile&lt;/h2&gt;
&lt;p&gt;Most &lt;code&gt;Dockerfile&lt;/code&gt; examples you&amp;rsquo;ll find are not &amp;ldquo;production ready&amp;rdquo; and they aren&amp;rsquo;t meant to be. They are for instructional purposes to help developers successfully build an image. But when one gets into production scenarios, there are several &amp;ldquo;good-to-know&amp;rdquo; and a few &amp;ldquo;have-to-know&amp;rdquo; techniques that will improve build times, security, and reliability.&lt;/p&gt;
&lt;p&gt;Let&amp;rsquo;s look at a typical example that you might run into if you&amp;rsquo;re a Node.js developer looking to get &amp;ldquo;Hello World&amp;rdquo; running with Docker. I won&amp;rsquo;t go through building an actual app - there are a lot of great examples out there to show you how to do this - but rather focus on what to do if you were going to ship this to production.&lt;/p&gt;
&lt;p&gt;The typical &lt;code&gt;Dockerfile&lt;/code&gt; in a &amp;ldquo;Hello World&amp;rdquo; example might look something like this:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-docker" data-lang="docker"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;FROM&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;node:latest&lt;/span&gt;&lt;span class="err"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;WORKDIR&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;/usr/src/app&lt;/span&gt;&lt;span class="err"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;COPY&lt;/span&gt; package*.json app.js ./&lt;span class="err"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;RUN&lt;/span&gt; npm install&lt;span class="err"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;EXPOSE&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;3000&lt;/span&gt;&lt;span class="err"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;CMD&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;&amp;#34;node&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;&amp;#34;app.js&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="err"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;It uses the latest version of the official Node.js image, sets a directory and copies your app into the container image, installs dependencies, exposes port 3000, and runs the app via &lt;code&gt;CMD&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;While this will run no problem on your local machine, and is great for learning the ropes, this approach is almost certainly going to run into issues when you ship it to production. Let&amp;rsquo;s take a look at some of these in order of severity.&lt;/p&gt;
&lt;h3 id="major-issues"&gt;Major issues&lt;/h3&gt;
&lt;h4 id="running-as-root"&gt;Running as Root&lt;/h4&gt;
&lt;p&gt;Since this example doesn&amp;rsquo;t set a &lt;code&gt;USER&lt;/code&gt; explicitly in the &lt;code&gt;Dockerfile&lt;/code&gt;, Docker runs the build and all commands as the &lt;code&gt;root&lt;/code&gt; user. While not an issue for local development, your friendly neighbourhood SysAdmin will tell you the myriad of issues that come with running applications as root on a server in production. And with Docker, a &lt;a href="https://medium.com/jobteaser-dev-team/docker-user-best-practices-a8d2ca5205f4"&gt;new set of attack methods&lt;/a&gt; can arise.&lt;/p&gt;
&lt;p&gt;Thankfully, most major languages and frameworks have a predefined user for running applications. In Node.js, the user is just &lt;code&gt;node&lt;/code&gt; and can be invoked in the &lt;code&gt;Dockerfile&lt;/code&gt; explicitly.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-docker" data-lang="docker"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;FROM&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;node:latest&lt;/span&gt;&lt;span class="err"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;WORKDIR&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;/usr/src/app&lt;/span&gt;&lt;span class="err"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;COPY&lt;/span&gt; package*.json app.js ./&lt;span class="err"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;RUN&lt;/span&gt; npm install&lt;span class="err"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="err"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;USER&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;node&lt;/span&gt;&lt;span class="err"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="err"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;EXPOSE&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;3000&lt;/span&gt;&lt;span class="err"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;CMD&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;&amp;#34;node&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;&amp;#34;app.js&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="err"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h4 id="using-latest-version"&gt;Using &lt;code&gt;latest&lt;/code&gt; version&lt;/h4&gt;
&lt;p&gt;Choosing a version number for your container is often called &lt;em&gt;pinning&lt;/em&gt;. While many tutorials - and even some experts - will counsel newcomers to pin their images to the &lt;code&gt;latest&lt;/code&gt; tag, which means you get whatever the most recently updated version is, using the &lt;code&gt;latest&lt;/code&gt; tag can cause issues in production.&lt;/p&gt;
&lt;p&gt;Containers are meant to be ephemeral, meaning they can be created, destroyed, started, stopped, and reproduced with ease and &lt;em&gt;reliability&lt;/em&gt;. Using the &lt;code&gt;latest&lt;/code&gt; tag means there isn&amp;rsquo;t a single source of truth for your container&amp;rsquo;s &amp;ldquo;bill of materials&amp;rdquo;. A new version or update of a dependency could introduce a breaking change, which may cause the build to fail somewhere in your CI/CD pipeline.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Example &lt;code&gt;Dockerfile&lt;/code&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-docker" data-lang="docker"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;FROM&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;node:latest&lt;/span&gt;&lt;span class="err"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;&lt;strong&gt;Production &lt;code&gt;Dockerfile&lt;/code&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-docker" data-lang="docker"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;FROM&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;node:16.2.0&lt;/span&gt;&lt;span class="err"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Other tutorials I&amp;rsquo;ve seen pin only the major version. For example, using &lt;code&gt;node:14&lt;/code&gt;. This carries the same risks as using &lt;code&gt;latest&lt;/code&gt;, as minor versions can change dependencies as well.&lt;/p&gt;
&lt;p&gt;Now, pinning a specific major and minor version in your &lt;code&gt;Dockerfile&lt;/code&gt; is a trade-off decision - you&amp;rsquo;re choosing to not automatically receive security, fixes or performance improvements that come via new updates - but most DevSecOps teams prefer to employ security scanning and container management software as a way to control updates rather than dealing with the unpredictability that comes with container build failures in production CI/CD pipelines.&lt;/p&gt;
&lt;h3 id="performance-improvements"&gt;Performance improvements&lt;/h3&gt;
&lt;h4 id="better-layer-caching"&gt;Better layer caching&lt;/h4&gt;
&lt;p&gt;Docker works on the concept of &lt;em&gt;layer caching&lt;/em&gt;. It builds images sequentially. Layering dependencies on top of each other and only rebuilding them when something in the layer has changed.&lt;/p&gt;
&lt;p&gt;Layer 0 in a Docker image is often the base operating system, which rarely changes significantly; although commercial Linux vendors often publish new base images to incorporate security fixes.&lt;/p&gt;
&lt;p&gt;Application code, however, is highly likely to change during the software development cycle, as you iterate on features, refactor, and fix bugs. Dependencies in our core system, installed here by &lt;code&gt;npm install&lt;/code&gt;, change more often than the base OS, but less often than the application code.&lt;/p&gt;
&lt;p&gt;In our example &lt;code&gt;Dockerfile&lt;/code&gt;, we simply need to break the installation of the dependencies into separate instructions on their own lines.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-docker" data-lang="docker"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;FROM&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;node:16.0.2&lt;/span&gt;&lt;span class="err"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;WORKDIR&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;/usr/src/app&lt;/span&gt;&lt;span class="err"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="err"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;COPY&lt;/span&gt; package*.json ./&lt;span class="err"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;RUN&lt;/span&gt; npm ci&lt;span class="err"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="err"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;USER&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;node&lt;/span&gt;&lt;span class="err"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="err"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;COPY&lt;/span&gt; app.js ./&lt;span class="err"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="err"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;EXPOSE&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;3000&lt;/span&gt;&lt;span class="err"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;CMD&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;&amp;#34;node&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;&amp;#34;app.js&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="err"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;We actually end up creating another layer by now having two &lt;code&gt;COPY&lt;/code&gt; commands. While adding layers is typically a no-no for build times and image sizes, the tax we pay on this optimisation is going to save us in the long run as we cycle through the QA process, since we aren&amp;rsquo;t reinstalling dependencies if we don’t have to.&lt;/p&gt;
&lt;p&gt;We also opt for the &lt;code&gt;npm ci&lt;/code&gt; command instead of &lt;code&gt;npm install&lt;/code&gt;, which is preferred for automated environments, such as CI/CD, and will help prevent breaking changes from dependencies. Read &lt;a href="https://docs.npmjs.com/cli/v7/commands/npm-ci"&gt;more about &lt;code&gt;npm ci&lt;/code&gt; here&lt;/a&gt;.&lt;/p&gt;
&lt;h4 id="use-entrypoint-instead-of-cmd"&gt;Use &lt;code&gt;ENTRYPOINT&lt;/code&gt; instead of &lt;code&gt;CMD&lt;/code&gt;&lt;/h4&gt;
&lt;p&gt;At a surface level, there isn&amp;rsquo;t a big difference between using &lt;code&gt;ENTRYPOINT&lt;/code&gt; with your app file versus running &lt;code&gt;CMD&lt;/code&gt; using the shell plus your app file. However, web- and API-type containers like Node.js applications are often running as executables in production, and there, proper signal handling - such as graceful shutdowns - are important.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;CMD&lt;/code&gt; provides some flexibility for calling executables with flags or overwriting them, which is common in development. But that generally won&amp;rsquo;t be relevant to production instances and &lt;code&gt;ENTRYPOINT&lt;/code&gt; will likely provide better signal processing.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-docker" data-lang="docker"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;FROM&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;node:16.0.2&lt;/span&gt;&lt;span class="err"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;WORKDIR&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;/usr/src/app&lt;/span&gt;&lt;span class="err"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="err"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;COPY&lt;/span&gt; package*.json ./&lt;span class="err"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;RUN&lt;/span&gt; npm ci&lt;span class="err"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="err"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;USER&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;node&lt;/span&gt;&lt;span class="err"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="err"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;COPY&lt;/span&gt; app.js ./&lt;span class="err"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="err"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;EXPOSE&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;3000&lt;/span&gt;&lt;span class="err"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;ENTRYPOINT&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;&amp;#34;node&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;&amp;#34;app.js&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="err"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h3 id="cleaning-up-cached-files"&gt;Cleaning up cached files&lt;/h3&gt;
&lt;p&gt;Most package managers have the ability to clean up their own cache. If you don’t do this, you&amp;rsquo;ll just be moving a bunch of unused files into your container for no reason. It might not save a lot of space depending on your application, but think of it as dropping your unused items at the charity shop &lt;em&gt;before&lt;/em&gt; you move rather than loading them in the moving van. It&amp;rsquo;s not a lot of effort and it&amp;rsquo;s the right thing to do. We do this by adding &lt;code&gt;&amp;amp;&amp;amp; npm cache clean --force&lt;/code&gt; to our &lt;code&gt;RUN&lt;/code&gt; instruction.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-docker" data-lang="docker"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;FROM&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;node:16.0.2&lt;/span&gt;&lt;span class="err"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;WORKDIR&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;/usr/src/app&lt;/span&gt;&lt;span class="err"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;COPY&lt;/span&gt; package*.json ./&lt;span class="err"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="err"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;RUN&lt;/span&gt; npm ci &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; npm cache clean --force&lt;span class="err"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="err"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;USER&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;node&lt;/span&gt;&lt;span class="err"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="err"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;COPY&lt;/span&gt; app.js ./&lt;span class="err"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="err"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;EXPOSE&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;3000&lt;/span&gt;&lt;span class="err"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;ENTRYPOINT&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;&amp;#34;node&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;&amp;#34;app.js&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="err"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h3 id="conclusions"&gt;Conclusions&lt;/h3&gt;
&lt;p&gt;Improving your &lt;code&gt;Dockerfile&lt;/code&gt; is the first step towards creating a slimmed and optimised container. It closes some major security loopholes that are likely to raise flags with downstream checks and adds baseline optimisations for build time and docker image size.&lt;/p&gt;
&lt;p&gt;If this is all you do to improve your containers prior to shipping to production, you won&amp;rsquo;t be in a bad spot, but there&amp;rsquo;s more - &lt;em&gt;way more&lt;/em&gt; - that you can do to optimise images. We&amp;rsquo;ll &lt;a href="https://blog.wimpys.world/posts/creating-production-ready-containers-advanced-techniques/"&gt;explore those techniques in the next article&lt;/a&gt;.&lt;/p&gt;</description><summary>Beginners guide to container best practices</summary></item></channel></rss>