{"id":95951,"date":"2020-10-23T11:00:29","date_gmt":"2020-10-23T08:00:29","guid":{"rendered":"https:\/\/en.buradabiliyorum.com\/what-are-docker-volumes-and-how-do-you-use-them-cloudsavvy-it\/"},"modified":"2020-10-23T11:00:29","modified_gmt":"2020-10-23T08:00:29","slug":"what-are-docker-volumes-and-how-do-you-use-them-cloudsavvy-it","status":"publish","type":"post","link":"https:\/\/buradabiliyorum.com\/en\/what-are-docker-volumes-and-how-do-you-use-them-cloudsavvy-it\/","title":{"rendered":"#What Are Docker Volumes, and How Do You Use Them? \u2013 CloudSavvy IT"},"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-6a278221cfd32\" 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-6a278221cfd32\" 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\/what-are-docker-volumes-and-how-do-you-use-them-cloudsavvy-it\/#What_Are_Volumes\" >What Are Volumes?<\/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\/what-are-docker-volumes-and-how-do-you-use-them-cloudsavvy-it\/#How_Do_You_Use_Them\" >How Do You Use Them?<\/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\/what-are-docker-volumes-and-how-do-you-use-them-cloudsavvy-it\/#Transfering_Docker_Volumes\" >Transfering Docker Volumes<\/a><\/li><\/ul><\/nav><\/div>\n<p><strong>&#8220;#What Are Docker Volumes, and How Do You Use Them? \u2013 CloudSavvy IT&#8221;<\/strong><\/p>\n<div id=\"article-content-area\">\n<img loading=\"lazy\" decoding=\"async\" class=\"imgchk9 alignnone wp-image-503 size-full\" src=\"https:\/\/www.cloudsavvyit.com\/thumbcache\/0\/0\/576bdb68675c300a17d82c131b23c927\/p\/uploads\/2019\/06\/c454d054.png\" alt=\"Docker logo.\" width=\"700\" height=\"300\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>By default, Docker containers are entirely stateless. The data stored on the container is ephemeral, and will be wiped when the container shuts down. This is obviously not ideal for many <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, so Docker provides a few ways of dealing with state.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"What_Are_Volumes\"><\/span>What Are Volumes?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The simplest way of making Docker data persistent is\u00a0<a rel=\"nofollow noopener noreferrer\" target=\"_blank\" href=\"https:\/\/docs.docker.com\/storage\/bind-mounts\/\"><em>bind mounts<\/em><\/a>, which literally bind a location on the host\u2019s disk to a location on the container\u2019s disk. These are simple to create and use, but are a little janky as you need to set up the directories and manage them yourself.<\/p>\n<p>Volumes are like virtual hard drives managed by Docker. Docker handles storing them on disk (usually in <code>\/var\/lib\/docker\/volumes\/<\/code>), and gives them an easily memorable single name rather than a directory path. It\u2019s easy to create and remove them using the Docker CLI.<\/p>\n<p>These have a few other benefits besides being managed by Docker. They work on both Linux and Windows containers, they can\u00a0more safely shared among multiple containers, and the volume drivers allow cloud providers to provide remote hosting for the underlying data. Overall, volumes are easier to use than bind mounts, and are recommended by Docker for managing state.<\/p>\n<p>Of course, you should consider if you really need Docker data to be stored on the server. For many applications, using an external remote data store like Amazon S3 or an external database is enough to store the data they use without tying it up on the frontend server.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"How_Do_You_Use_Them\"><\/span>How Do You Use Them?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>You can create a new volume from the command line with the following command:<\/p>\n<pre>docker volume create nginx-config<\/pre>\n<p>And then, when you go to run your Docker container, link it to the target in the container with the <code>--mount<\/code>\u00a0flag:<\/p>\n<pre>docker run -d &#13;\n--name devtest &#13;\n--mount source=nginx-config,target=\/etc\/nginx &#13;\nnginx:latest<\/pre>\n<p>If you run <code>docker inspect &lt;name&gt;<\/code>, you\u2019ll see the volume listed under the <code>Mounts<\/code>\u00a0section.<\/p>\n<p>If you\u2019re using Docker Compose, the setup is easy as well. Simply add a <code>volumes<\/code>\u00a0entry for each container service you have, then map a volume name to a location in the guest. You\u2019ll also need to provide a list of volumes in a top-level <code>volumes<\/code>\u00a0key for Compose to provision.<\/p>\n<pre>version:\u00a0<span class=\"st0\">\"3.0\"<\/span>&#13;\nservices:&#13;\n  web:&#13;\n    image: nginx:latest&#13;\n    ports:&#13;\n      -\u00a0<span class=\"st0\">\"80:80\"<\/span>&#13;\n    volumes:&#13;\n      - nginx-config:<span class=\"sy0\">\/<\/span>etc<span class=\"sy0\">\/<\/span>nginx<span class=\"sy0\">\/<\/span>&#13;\nvolumes:&#13;\n  nginx-config:<\/pre>\n<p>This will create the volume automatically for this Compose. If you\u2019d like to use a premade volume from outside Compose, specify <code>external: true<\/code>\u00a0in the volume configuration:<\/p>\n<pre>volumes:&#13;\n  cms-content:&#13;\n    external: true<\/pre>\n<p>If you\u2019d like to instead simply do a bind mount and not bother with volumes, simply enter in a path name in place of the volume name, and forego defining the volume names.<\/p>\n<pre>version:\u00a0\"3.0\"&#13;\nservices:&#13;\n  web:&#13;\n    image: nginx:latest&#13;\n    ports:&#13;\n      -\u00a0\"80:80\"&#13;\n    volumes:&#13;\n      - \/docker\/nginx-config\/:\/etc\/nginx\/<\/pre>\n<p>You can read <a rel=\"nofollow noopener noreferrer\" target=\"_blank\" href=\"https:\/\/docs.docker.com\/compose\/compose-file\/#volumes\">Docker\u2019s full documentation on using volumes with Compose<\/a> if your use case requires something more specific than this.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Transfering_Docker_Volumes\"><\/span>Transfering Docker Volumes<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Docker volumes are just folders created automatically and stored at <code>\/var\/lib\/docker\/volumes\/<\/code>, with each volume being stored under <code>.\/volumename\/_data\/<\/code>. To back up and restore, you can simply backup these volumes directly.<\/p>\n<p>If you\u2019d instead like to use the Docker CLI, they don\u2019t provide an easy way to do this unfortunately. To back up a container\u2019s volumes, you\u2019ll need the container name, as well as the mount location that the data is stored in.<\/p>\n<p>The way to do it is a bit of a hack\u2014you\u2019ll need to run a command in a new Docker container, bind mount the current shell directory, mount the volume to that container, then <code>tar<\/code>\u00a0the directory into a backup. You\u2019ll end up with a <code>backup.tar<\/code>\u00a0of the volume:<\/p>\n<pre>docker run --rm --volumes-from containername -v $(pwd):\/backup ubuntu tar cvf \/backup\/backup.tar \/mountlocation<\/pre>\n<p>Then, similarly, you can extract the archive in the target directory:<\/p>\n<pre>docker run --rm --volumes-from containername -v $(pwd):\/backup ubuntu bash -c \"cd \/mountlocation &amp;&amp; tar xvf \/backup\/backup.tar --s<a href=\"https:\/\/buradabiliyorum.com\/en\/category\/trip-and-travel\/\" data-internallinksmanager029f6b8e52c=\"10\" title=\"Trip &amp; Travel\" target=\"_blank\" rel=\"noopener\">trip<\/a> 1\"<\/pre>\n<p>This is still better than having to know the host location though, so you can automate this if you want to.\n<\/p><\/div>\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 noreferrer\">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 noreferrer\">Technology category.<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<p><span style=\"color: black;\"><a style=\"color: #ff9900;\" href=\"https:\/\/www.cloudsavvyit.com\/7500\/what-are-docker-volumes-and-how-do-you-use-them\/\" target=\"_blank\" rel=\"noopener noreferrer\">Source<\/a><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;#What Are Docker Volumes, and How Do You Use Them? \u2013 CloudSavvy IT&#8221; By default, Docker containers are entirely stateless. The data stored on the container is ephemeral, and will be wiped when the container shuts down. This is obviously not ideal for many applications, so Docker provides a few ways of dealing with state&#8230;.<\/p>\n","protected":false},"author":1,"featured_media":95952,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"fifu_image_url":"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2019\/06\/c454d054.png","fifu_image_alt":"","footnotes":""},"categories":[18],"tags":[],"class_list":["post-95951","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\/95951","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=95951"}],"version-history":[{"count":0,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/95951\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media\/95952"}],"wp:attachment":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media?parent=95951"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/categories?post=95951"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/tags?post=95951"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}