{"id":489598,"date":"2022-09-02T03:48:40","date_gmt":"2022-09-02T00:48:40","guid":{"rendered":"https:\/\/en.buradabiliyorum.com\/understanding-the-dockerfile-volume-instruction\/"},"modified":"2022-09-02T03:48:40","modified_gmt":"2022-09-02T00:48:40","slug":"understanding-the-dockerfile-volume-instruction","status":"publish","type":"post","link":"https:\/\/buradabiliyorum.com\/en\/understanding-the-dockerfile-volume-instruction\/","title":{"rendered":"#Understanding the Dockerfile VOLUME Instruction"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_84 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-6a2d832977ef1\" 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-6a2d832977ef1\" 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\/understanding-the-dockerfile-volume-instruction\/#%E2%80%9CUnderstanding_the_Dockerfile_VOLUME_Instruction%E2%80%9D\" >&#8220;Understanding the Dockerfile VOLUME Instruction&#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\/understanding-the-dockerfile-volume-instruction\/#Defining_Volumes_In_Dockerfiles\" >Defining Volumes In Dockerfiles<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/buradabiliyorum.com\/en\/understanding-the-dockerfile-volume-instruction\/#Populating_Initial_Volume_Content\" >Populating Initial Volume Content<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/buradabiliyorum.com\/en\/understanding-the-dockerfile-volume-instruction\/#Overriding_VOLUME_Instructions_When_Starting_a_Container\" >Overriding VOLUME Instructions When Starting a Container<\/a><\/li><\/ul><\/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-dockerfile-volume-instruction\/#When_Should_You_Use_VOLUME_Instructions\" >When Should You Use VOLUME Instructions?<\/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-dockerfile-volume-instruction\/#VOLUME_Pitfalls\" >VOLUME Pitfalls<\/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\/understanding-the-dockerfile-volume-instruction\/#Summary\" >Summary<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n<h1><span class=\"ez-toc-section\" id=\"%E2%80%9CUnderstanding_the_Dockerfile_VOLUME_Instruction%E2%80%9D\"><\/span>&#8220;Understanding the Dockerfile VOLUME Instruction&#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 volumes are used to store persistent data outside your containers. They allow config files, databases, and caches used by 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>lication to outlive individual container instances.<\/p>\n<p>Volumes can be mounted when you start containers with the <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/docs.docker.com\/engine\/reference\/run\"><code>docker run<\/code> command\u2019s<\/a> <code>-v<\/code> flag. This can either reference a named volume or bind mount a host directory into the container\u2019s filesystem.<\/p>\n<p>It\u2019s also possible to define volumes at image build time by using the <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/docs.docker.com\/engine\/reference\/builder\/#volume\"><code>VOLUME<\/code> instruction<\/a> in your Dockerfiles. This mechanism guarantees that containers started from the image will have persistent storage available. In this article you\u2019ll learn how to use this instruction and the use cases where it makes sense.<\/p>\n<h2 id=\"defining-volumes-in-dockerfiles\"><span class=\"ez-toc-section\" id=\"Defining_Volumes_In_Dockerfiles\"><\/span>Defining Volumes In Dockerfiles<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The Dockerfile <code>VOLUME<\/code> instruction creates a volume mount point at a specified container path. A volume will be mounted from your Docker host\u2019s filesystem each time a container starts.<\/p>\n<p>The Dockerfile in the following example defines a volume at the <code>\/opt\/app\/data<\/code> container path. New containers will automatically mount a volume to the directory.<\/p>\n<pre>FROM ubuntu:22.04&#13;\nVOLUME \/opt\/app\/data<\/pre>\n<p>Build your image so you can test the volume mount:<\/p>\n<pre>$ docker build -t volumes-test:latest .<\/pre>\n<p>Retrieve the list of existing volumes as a reference:<\/p>\n<pre>$ docker volume ls&#13;\nDRIVER   VOLUME NAME&#13;\nlocal    demo-volume<\/pre>\n<p>Now start a container using your test image:<\/p>\n<pre>$ docker run -it volume-test:latest&#13;\nroot@07be7bde68c2:\/#<\/pre>\n<p>Repeat the <code>docker volume ls<\/code> command to confirm a new volume has been created:<\/p>\n<pre>$ docker volume ls&#13;\nDRIVER   VOLUME NAME&#13;\nlocal    3198bf857fdcbb8758c5ec7049f2e31a40b79e329f756a68725d83e46976b7a8&#13;\nlocal    demo-volume<\/pre>\n<p>Exit out of your test container\u2019s shell so that the container stops:<\/p>\n<pre>root@07be7bde68c2:\/# exit&#13;\nexit<\/pre>\n<p>The volume and its data will continue to persist:<\/p>\n<pre>$ docker volume ls&#13;\nDRIVER   VOLUME NAME&#13;\nlocal    3198bf857fdcbb8758c5ec7049f2e31a40b79e329f756a68725d83e46976b7a8&#13;\nlocal    demo-volume<\/pre>\n<p>You can define multiple volumes in one instruction as a space-delimited string or a JSON array. Both of the following forms create and mount two unique volumes when containers start:<\/p>\n<pre>VOLUME \/opt\/app\/data \/opt\/app\/config&#13;\n# OR&#13;\nVOLUME [\"\/opt\/app\/data\", \"\/opt\/app\/config\"]<\/pre>\n<h3 id=\"populating-initial-volume-content\"><span class=\"ez-toc-section\" id=\"Populating_Initial_Volume_Content\"><\/span>Populating Initial Volume Content<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Volumes are automatically populated with content placed into the mount directory by previous image build steps:<\/p>\n<pre>FROM ubuntu:22.04&#13;\nCOPY default-config.yaml \/opt\/app\/config\/default-config.yaml&#13;\nVOLUME \/opt\/app\/config<\/pre>\n<p>This Dockerfile defines a volume that will be initialized with the existing <code>default-config.yaml<\/code> file. The container will be able to read <code>\/opt\/app\/config\/default-config.yaml<\/code> without having to check whether the file exists.<\/p>\n<p>Changes to a volume\u2019s content made <em>after<\/em> the <code>VOLUME<\/code> instruction will be discarded. In this example, the <code>default-config.yaml<\/code> file is still available after containers start because the <code>rm<\/code> command comes after <code>\/opt\/app\/config<\/code> is marked as a volume.<\/p>\n<pre>FROM ubuntu:22.04&#13;\nCOPY default-config.yaml \/opt\/app\/config\/default-config.yaml&#13;\nVOLUME \/opt\/app\/config&#13;\nRUN rm \/opt\/app\/config\/default-config.yaml<\/pre>\n<h3 id=\"overriding-volume-instructions-when-starting-a-container\"><span class=\"ez-toc-section\" id=\"Overriding_VOLUME_Instructions_When_Starting_a_Container\"><\/span>Overriding VOLUME Instructions When Starting a Container<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Volumes created by the <code>VOLUME<\/code> instruction are automatically named with a long unique hash. It\u2019s not possible to change their names so it can be difficult to identify which volumes are actively used by your containers.<\/p>\n<p>You can prevent these volumes appearing by manually defining volumes on your containers with <code>docker run -v<\/code> as usual. The following command explicitly mounts a named volume to the container\u2019s <code>\/opt\/app\/config<\/code> directory, making the Dockerfile\u2019s <code>VOLUME<\/code> instruction redundant.<\/p>\n<pre>$ docker run -it -v config:\/opt\/app\/config volumes-test:latest<\/pre>\n<h2 id=\"when-should-you-use-volume-instructions\"><span class=\"ez-toc-section\" id=\"When_Should_You_Use_VOLUME_Instructions\"><\/span>When Should You Use VOLUME Instructions?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><code>VOLUME<\/code> instructions can be helpful in situations where you want to enforce that persistence is used, such as in images that package a database server or file store. Using <code>VOLUME<\/code> instructions makes it easier to start containers without remembering the <code>-v<\/code> flags to apply.<\/p>\n<p><code>VOLUME<\/code> also serves as documentation of the container paths that store persistent data. Including these instructions in your Dockerfile allows anyone to determine where your container keeps its data, even if they\u2019re unfamiliar with your application.<\/p>\n<h2 id=\"volume-pitfalls\"><span class=\"ez-toc-section\" id=\"VOLUME_Pitfalls\"><\/span>VOLUME Pitfalls<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><code>VOLUME<\/code> isn\u2019t without its drawbacks. Its biggest problem is how it interacts with image builds. Using an image with a <code>VOLUME<\/code> instruction as your build\u2019s base image will behave unexpectedly if you change content within the volume mount point.<\/p>\n<p>The gotcha from earlier still applies: the effects of commands after the <code>VOLUME<\/code> instruction will be discarded. As <code>VOLUME<\/code> will reside in the base image, everything in your own Dockerfile comes after the instruction and you\u2019re unable to modify the directory\u2019s default contents. Behind the scenes, starting the temporary container for the build will create a new volume on your host that gets destroyed at the end of the build. Changes will not be copied back to the output image.<\/p>\n<p>Automatic volume mounting can be problematic in other situations too. Sometimes users might prefer to start a temporary container without any volumes, perhaps for evaluation or debugging purposes. <code>VOLUME<\/code> removes this possibility as it\u2019s not possible to disable the automatic mounts. This causes many redundant volumes to accumulate on the host if containers that use the instruction are regularly started.<\/p>\n<h2 id=\"summary\"><span class=\"ez-toc-section\" id=\"Summary\"><\/span>Summary<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Dockerfile <code>VOLUME<\/code> instructions allow volume mounts to be defined at image build time. They guarantee that containers started from the image will have persistent data storage available, even if the user omits the <code>docker run<\/code> command\u2019s <code>-v<\/code> flag.<\/p>\n<p>This behavior can be useful for images where persistence is critical or many volumes are needed. However the <code>VOLUME<\/code> instruction also breaks some user expectations and introduces unique behaviors so it needs to be written with care. Providing a Docker Compose file <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/docs.docker.com\/storage\/volumes\/#use-a-volume-with-docker-compose\">that automatically creates required volumes<\/a> is often a better solution.<\/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\/understanding-the-dockerfile-volume-instruction\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;Understanding the Dockerfile VOLUME Instruction&#8221; Docker volumes are used to store persistent data outside your containers. They allow config files, databases, and caches used by your application to outlive individual container instances. Volumes can be mounted when you start containers with the docker run command\u2019s -v flag. This can either reference a named volume or&#8230;<\/p>\n","protected":false},"author":1,"featured_media":489599,"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-489598","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\/489598","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=489598"}],"version-history":[{"count":0,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/489598\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media\/489599"}],"wp:attachment":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media?parent=489598"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/categories?post=489598"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/tags?post=489598"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}