{"id":485639,"date":"2022-08-18T03:48:48","date_gmt":"2022-08-18T00:48:48","guid":{"rendered":"https:\/\/en.buradabiliyorum.com\/how-to-mount-a-docker-volume-while-excluding-a-subdirectory\/"},"modified":"2022-08-18T03:48:48","modified_gmt":"2022-08-18T00:48:48","slug":"how-to-mount-a-docker-volume-while-excluding-a-subdirectory","status":"publish","type":"post","link":"https:\/\/buradabiliyorum.com\/en\/how-to-mount-a-docker-volume-while-excluding-a-subdirectory\/","title":{"rendered":"#How to Mount a Docker Volume While Excluding a Subdirectory"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_87 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-6aa07e6c3ffec\" 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-6aa07e6c3ffec\" 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-mount-a-docker-volume-while-excluding-a-subdirectory\/#%E2%80%9CHow_to_Mount_a_Docker_Volume_While_Excluding_a_Subdirectory%E2%80%9D\" >&#8220;How to Mount a Docker Volume While Excluding a Subdirectory&#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-mount-a-docker-volume-while-excluding-a-subdirectory\/#Why_Would_You_Do_This\" >Why Would You Do This?<\/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-mount-a-docker-volume-while-excluding-a-subdirectory\/#How_to_Exclude_Subdirectories_from_Docker_Volume_Mounts\" >How to Exclude Subdirectories from Docker Volume Mounts<\/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-mount-a-docker-volume-while-excluding-a-subdirectory\/#Excluding_Files\" >Excluding Files<\/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-mount-a-docker-volume-while-excluding-a-subdirectory\/#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-mount-a-docker-volume-while-excluding-a-subdirectory\/#Summary\" >Summary<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n<h1><span class=\"ez-toc-section\" id=\"%E2%80%9CHow_to_Mount_a_Docker_Volume_While_Excluding_a_Subdirectory%E2%80%9D\"><\/span>&#8220;How to Mount a Docker Volume While Excluding a Subdirectory&#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-803108\" data-pagespeed-no-defer=\"\" src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2022\/05\/Docker.jpeg?width=1198&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Docker logo\" width=\"1602\" height=\"902\"\/><\/p>\n<p>Docker Volumes provide persistent storage to your containers. Data stored in volumes is stored independently of containers so it can be restored after restarts and replacements. Volumes support use with multiple containers simultaneously, facilitating data sharing situations.<\/p>\n<p>Mounting a Docker volume makes its contents available at a specific directory path inside the target container. Everything within the volume\u2019s filesystem tree becomes accessible. This can create a challenge if you want to exclude specific subdirectories in the volume data. In this article, you\u2019ll learn a simple technique for mounting volumes while avoiding certain paths.<\/p>\n<h2 id=\"why-would-you-do-this\"><span class=\"ez-toc-section\" id=\"Why_Would_You_Do_This\"><\/span>Why Would You Do This?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The default behavior of mounting the entire volume is normally desirable. Volumes are intended to store data created by containers so their content should be relevant to your <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>lications.<\/p>\n<p>Volumes can also be populated using host bind mounts. These directly map a directory on your machine to a path inside your container. Changes made within the host directory will be automatically reflected in the container.<\/p>\n<p>Bind mounts are commonly used to accelerate application development. You can modify source code and observe your changes without having to rebuild your Docker image. Project working directories often contain some folders which you don\u2019t want to be mirrored though, such as <code>node_modules<\/code> and <code>vendor.<\/code> These may already exist in your container, populated during the image build steps. Excluding your local folders from being mounted lets you reliably test your code using the dependencies provided by your image.<\/p>\n<h2 id=\"how-to-exclude-subdirectories-from-docker-volume-mounts\"><span class=\"ez-toc-section\" id=\"How_to_Exclude_Subdirectories_from_Docker_Volume_Mounts\"><\/span>How to Exclude Subdirectories from Docker Volume Mounts<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Subdirectories can be excluded from a volume mount using a simple technique: create another mount at the path you want to ignore. If you\u2019re mounting <code>~\/app<\/code> to <code>\/opt\/app<\/code> in your container, you can exclude the <code>~\/app\/node_modules<\/code> directory by mounting a second empty volume to <code>\/opt\/app\/node_modules<\/code>:<\/p>\n<pre>$ docker run --name app \\&#13;\n    -v ~\/app:\/opt\/app \\&#13;\n    -v \/opt\/app\/node_modules \\&#13;\n    app-image:latest<\/pre>\n<p>This container will start with the contents of your host\u2019s <code>~\/app<\/code> directory accessible at <code>\/opt\/app<\/code>. However <code>\/opt\/app\/node_modules<\/code> will contain the original content provided by the base image, instead of your host\u2019s <code>~\/app\/node_modules<\/code> directory.<\/p>\n<p>This works because Docker automatically populates newly created empty volumes with the existing content of the destination path they\u2019re mounted to. If you run <code>npm install<\/code> as part of your Dockerfile, <code>\/opt\/app\/node_modules<\/code> will already contain all your dependencies. The first volume mount binds your host directory into the container but the second one overrides it with an empty volume at <code>\/opt\/app\/node_modules<\/code>. This is then populated with the files and folders included in the image.<\/p>\n<p>The order of your volume mounts is important \u2013 the subdirectory mount needs to apply after the less specific parent bind. Otherwise the contents of <code>~\/app<\/code>, including its version of <code>node_modules<\/code>, will end up overriding the empty volume that\u2019s meant to create the exclusion.<\/p>\n<h2 id=\"excluding-files\"><span class=\"ez-toc-section\" id=\"Excluding_Files\"><\/span>Excluding Files<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>You can use a similar technique to effectively exclude individual files. Mounting your host\u2019s <code>\/dev\/null<\/code> to the file path will blank it out, as if it had no content.<\/p>\n<pre>$ docker run --name app \\&#13;\n    -v \/dev\/null:\/opt\/app\/config.yaml \\&#13;\n    app-image:latest<\/pre>\n<p>This only works to exclude files \u2013 <code>\/dev\/null<\/code> won\u2019t map onto directory paths. The method doesn\u2019t work to omit a file while retaining the original version from your base image either. It\u2019ll map <code>\/dev\/null<\/code> onto the path, replacing any existing file that\u2019s there.<\/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>These techniques both work with Docker Compose too. Adjust your service definition\u2019s <code>volumes<\/code> section to include your regular bind mount and an appropriate empty volume override.<\/p>\n<div class=\"wp-geshi-highlight-wrap5\">\n<div class=\"wp-geshi-highlight-wrap4\">\n<div class=\"wp-geshi-highlight-wrap3\">\n<div class=\"wp-geshi-highlight-wrap2\">\n<div class=\"wp-geshi-highlight-wrap\">\n<div class=\"wp-geshi-highlight\">\n<div class=\"yaml\">\n<pre class=\"de1\"><span class=\"co4\">services<\/span>:<span class=\"co4\">\n  app<\/span>:<span class=\"co3\">\n    image<\/span><span class=\"sy2\">: <\/span>app-image:latest<span class=\"co3\">\n    build<\/span><span class=\"sy2\">: <\/span>.<span class=\"co4\">\n    volumes<\/span><span class=\"sy2\">:\n<\/span>      - ~\/app:\/opt\/app\n      - \/dev\/null:\/opt\/app\/config.yaml      <span class=\"co1\"># Exclude file<\/span>\n      - \/opt\/app\/node_modules               <span class=\"co1\"># Exclude directory<\/span><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>Running <code>docker-compose up<\/code> will have the same effect as the plain <code>docker run<\/code> example shown above.<\/p>\n<h2 id=\"summary\"><span class=\"ez-toc-section\" id=\"Summary\"><\/span>Summary<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Docker volume mounts replace everything in the container\u2019s destination path with the contents of the bound host directory. In some situations you might want to customize this behavior by excluding specific host paths from being mounted.<\/p>\n<p>In this article you\u2019ve seen how mounting an empty volume to a subdirectory inside the container will override the first bind. The content of the container subdirectory will revert to the original content provided by your image. A variation of this technique can also be used for files, although the path will be overwritten with <code>\/dev\/null<\/code> instead.<\/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-mount-a-docker-volume-while-excluding-a-subdirectory\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;How to Mount a Docker Volume While Excluding a Subdirectory&#8221; Docker Volumes provide persistent storage to your containers. Data stored in volumes is stored independently of containers so it can be restored after restarts and replacements. Volumes support use with multiple containers simultaneously, facilitating data sharing situations. Mounting a Docker volume makes its contents available&#8230;<\/p>\n","protected":false},"author":1,"featured_media":485640,"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.jpeg?height=200p&trim=2,2,2,2","fifu_image_alt":"","footnotes":""},"categories":[18],"tags":[],"class_list":["post-485639","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\/485639","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=485639"}],"version-history":[{"count":0,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/485639\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media\/485640"}],"wp:attachment":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media?parent=485639"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/categories?post=485639"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/tags?post=485639"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}