{"id":210777,"date":"2021-03-25T15:00:53","date_gmt":"2021-03-25T12:00:53","guid":{"rendered":"https:\/\/en.buradabiliyorum.com\/understanding-the-docker-build-context-why-you-should-use-dockerignore-cloudsavvy-it\/"},"modified":"2021-03-25T15:00:53","modified_gmt":"2021-03-25T12:00:53","slug":"understanding-the-docker-build-context-why-you-should-use-dockerignore-cloudsavvy-it","status":"publish","type":"post","link":"https:\/\/buradabiliyorum.com\/en\/understanding-the-docker-build-context-why-you-should-use-dockerignore-cloudsavvy-it\/","title":{"rendered":"#Understanding the Docker Build Context (Why You Should Use Dockerignore) \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-6a3a20a2cedba\" 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-6a3a20a2cedba\" 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\/understanding-the-docker-build-context-why-you-should-use-dockerignore-cloudsavvy-it\/#What_Is_the_Build_Context\" >What Is the Build Context?<\/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\/understanding-the-docker-build-context-why-you-should-use-dockerignore-cloudsavvy-it\/#Why_Is_the_Build_Context_Used\" >Why Is the Build Context Used?<\/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\/understanding-the-docker-build-context-why-you-should-use-dockerignore-cloudsavvy-it\/#Excluding_Resources_from_the_Build_Context\" >Excluding Resources from the Build Context<\/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\/understanding-the-docker-build-context-why-you-should-use-dockerignore-cloudsavvy-it\/#Other_Build_Context_Issues\" >Other Build Context Issues<\/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\/understanding-the-docker-build-context-why-you-should-use-dockerignore-cloudsavvy-it\/#Compressing_the_Build_Context\" >Compressing the Build Context<\/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\/understanding-the-docker-build-context-why-you-should-use-dockerignore-cloudsavvy-it\/#Conclusion\" >Conclusion<\/a><\/li><\/ul><\/nav><\/div>\n<p><strong>&#8220;#Understanding the Docker Build Context (Why You Should Use Dockerignore) \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>The Docker build context refers to the files and directories that will be available to the Docker engine when you run <code>docker build<\/code>. Anything not included in the build context won\u2019t be accessible to commands in your <code>Dockerfile<\/code>.<\/p>\n<p>You should audit your use of <code>docker build<\/code> to keep your build contexts small. Accidentally including unnecessary files can result in an excessively large build context, which will lead to longer builds.<\/p>\n<h2 id=\"what-is-the-build-context\"><span class=\"ez-toc-section\" id=\"What_Is_the_Build_Context\"><\/span>What Is the Build Context?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Here\u2019s a simple <code>docker build<\/code> command:<\/p>\n<pre><code>docker build . -t my-image:latest<\/code><\/pre>\n<p>This builds a Docker image using the <code>Dockerfile<\/code> found in your working directory. The resulting image will be tagged as <code>my-image:latest<\/code>, although this detail isn\u2019t important to this tutorial.<\/p>\n<p>Within your <code>Dockerfile<\/code>, you\u2019ll likely use <code>COPY<\/code> to add files and folders into your image:<\/p>\n<pre class=\"dockerfile\"><code>FROM httpd:latest&#13;\n&#13;\nCOPY index.html \/usr\/local\/apache2\/htdocs\/index.html&#13;\nCOPY css\/ \/usr\/local\/apache2\/htdocs\/css\/<\/code><\/pre>\n<p>This example copies the <code>index.html<\/code> file and <code>css<\/code> directory into your container. At first glance, it looks like the <code>COPY<\/code> statement simply references a path that\u2019s resolved relative to your working directory.<\/p>\n<p>This isn\u2019t quite the case. <code>COPY<\/code> can only access resources available in the build context. In this example, the build context is the working directory, so the files and folders within it are available. By default, Docker uses the contents of the directory passed to <code>docker build<\/code> as the build context.<\/p>\n<h2 id=\"why-is-the-build-context-used\"><span class=\"ez-toc-section\" id=\"Why_Is_the_Build_Context_Used\"><\/span>Why Is the Build Context Used?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The build context is important because the Docker CLI and Docker Engine might not be running on the same machine. When you run <code>docker build<\/code>, the CLI sends the files and folders to build to the Docker engine. This set of files and folders becomes the build context.<\/p>\n<p>Furthermore, not every build context is as straightforward as reusing your working directory. Docker also supports Git repository URLs as the path given to <code>docker build<\/code>. In this case, the build context becomes the content of the specified repository.<\/p>\n<p>The build context\u2019s default \u201cinclude all\u201d behavior is fine for many small repositories. Problems become <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>arent once you add files to your working directory that aren\u2019t used by your <code>Dockerfile<\/code>. Resources such as prebuilt binaries, documentation files, and dependency libraries will be included in the build context even though they\u2019re redundant.<\/p>\n<p>Including too many assets in the build context can become a performance drain. You\u2019re needlessly copying files that will never be used. The slowdown will be particularly evident if you\u2019re connected to a remote Docker daemon or if you\u2019re using a slow mechanical hard drive. You\u2019ll see \u201csending build context to Docker daemon\u201d in your shell while the copy is completed.<\/p>\n<p>Docker does try to minimize redundant copying on its own. The BuildKit build backend\u2014used <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/docs.docker.com\/engine\/reference\/builder\">since Docker 18.09<\/a>\u2014added support for incremental transfers. This means that Docker will usually only need to copy files added or changed since your last build. It\u2019ll still copy the whole lot on the first build.<\/p>\n<h2 id=\"excluding-resources-from-the-build-context\"><span class=\"ez-toc-section\" id=\"Excluding_Resources_from_the_Build_Context\"><\/span>Excluding Resources from the Build Context<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>To resolve wasteful copying for good, you must tell Docker what it can omit from the build context. Let\u2019s start by creating a <code>Dockerfile<\/code>:<\/p>\n<pre class=\"dockerfile\"><code>FROM node:latest&#13;\nWORKDIR \/my-app&#13;\nCOPY package.json package.json&#13;\nCOPY package-lock.json package-lock.json&#13;\nCOPY src\/ .&#13;\nRUN npm install<\/code><\/pre>\n<p>This simple <code>Dockerfile<\/code> could be used by an application written in Node.js. Node.js programs use <code>npm<\/code> as their package manager. Packages are installed to a <code>node_modules<\/code> folder. When you run <code>npm install<\/code> locally, during development, the packages will be downloaded to the <code>node_modules<\/code> folder in your working directory.<\/p>\n<p>The <code>Dockerfile<\/code> runs <code>npm install<\/code> itself to acquire the dependencies. This ensures that the image is fully self-contained. There\u2019s no need to copy in the <em>local<\/em> <code>node_modules<\/code> folder, as it\u2019s not used by the <code>Dockerfile<\/code>.<\/p>\n<p>Despite this, Docker will still include the <code>node_modules<\/code> folder in the default build context. To exclude it, create a <code>.dockerignore<\/code> file in your working directory. This file has a similar syntax to <code>.gitignore<\/code>.<\/p>\n<pre><code>node_modules\/<\/code><\/pre>\n<p>Any paths listed in <code>.dockerignore<\/code> will be excluded from the build context. You should make sure that\u00a0<code>.dockerignore<\/code> is kept updated with changes to your project\u2019s filesystem structure. You can substantially reduce Docker build context copying time by checking that only relevant paths (those actually used by your <code>Dockerfile<\/code>) are present in the build context.<\/p>\n<p>In the case of our example, the <code>node_modules<\/code> folder could include thousands of files if we have a lot of dependencies in our project. Copying them to the Docker daemon as part of the build context could take several seconds and would be a wasteful operation. The <code>Dockerfile<\/code> completely ignores them, fetching its own dependencies via <code>npm install<\/code> instead.<\/p>\n<h2 id=\"other-build-context-issues\"><span class=\"ez-toc-section\" id=\"Other_Build_Context_Issues\"><\/span>Other Build Context Issues<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Not using <code>.dockerignore<\/code> can introduce other issues, too. A Dockerfile with this line is particularly problematic:<\/p>\n<pre class=\"dockerfile\"><code>COPY . \/my-app<\/code><\/pre>\n<p>This will copy <em>everything<\/em> in your working directory. This might seem like a good idea until you realize that your <code>.git<\/code> history and any secret files will also end up within your container.<\/p>\n<p>Copying an unfiltered build context also prevents Docker layer caching from working effectively. As <em>something<\/em> in your working directory will probably change between builds, Docker would need to run the <code>COPY<\/code> instruction every time. This would create a new layer\u2014and new layers for any subsequent instructions\u2014even if the assets you\u2019re interested in haven\u2019t changed.<\/p>\n<h2 id=\"compressing-the-build-context\"><span class=\"ez-toc-section\" id=\"Compressing_the_Build_Context\"><\/span>Compressing the Build Context<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>You can compress the build context to further improve build performance. Pass the <code>--compress<\/code> flag to <code>docker build<\/code> to apply gzip compression. The compression occurs before the context is sent to the Docker daemon.<\/p>\n<pre><code>docker build . -t my-image:latest --compress<\/code><\/pre>\n<p>This can improve performance in some scenarios. The compression adds its own overheads, though\u2014your system now needs to compress the context, and the receiving Docker daemon has to uncompress it. Using compression could actually be slower than copying the original files, in some circumstances. Experiment with each of your images to assess whether you see an improvement.<\/p>\n<h2 id=\"conclusion\"><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The Docker build context defines the files that will be available for copying in your <code>Dockerfile<\/code>. The build context is copied over to the Docker daemon before the build begins.<\/p>\n<p>Build contexts default to including the contents of the directory or Git repository you passed to <code>docker build<\/code>. You can omit items from the build context by creating a <code>.dockerignore<\/code> file. This increases efficiency by reducing the amount of redundant data passed to the Docker daemon.\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\/10271\/understanding-the-docker-build-context-why-you-should-use-dockerignore\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;#Understanding the Docker Build Context (Why You Should Use Dockerignore) \u2013 CloudSavvy IT&#8221; The Docker build context refers to the files and directories that will be available to the Docker engine when you run docker build. Anything not included in the build context won\u2019t be accessible to commands in your Dockerfile. You should audit your&#8230;<\/p>\n","protected":false},"author":1,"featured_media":210778,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"fifu_image_url":"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/01\/6dc7b5a0.jpeg","fifu_image_alt":"","footnotes":""},"categories":[18],"tags":[],"class_list":["post-210777","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\/210777","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=210777"}],"version-history":[{"count":0,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/210777\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media\/210778"}],"wp:attachment":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media?parent=210777"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/categories?post=210777"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/tags?post=210777"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}