{"id":832,"date":"2020-07-31T07:10:03","date_gmt":"2020-07-31T07:10:03","guid":{"rendered":"https:\/\/blog.samarthya.me\/wps\/?p=832"},"modified":"2020-07-31T07:27:15","modified_gmt":"2020-07-31T07:27:15","slug":"shared-volume","status":"publish","type":"post","link":"https:\/\/blog.samarthya.me\/wps\/2020\/07\/31\/shared-volume\/","title":{"rendered":"Shared Volume"},"content":{"rendered":"<p><a href=\"https:\/\/docs.docker.com\/storage\/images\/types-of-mounts-volume.png\"><img fetchpriority=\"high\" decoding=\"async\" class=\"aligncenter size-medium\" src=\"https:\/\/docs.docker.com\/storage\/images\/types-of-mounts-volume.png\" width=\"502\" height=\"255\"><\/a><\/p>\n<p>Volumes are managed by Docker and is the preferred way of persisting, sharing data.<\/p>\n<h2>Key Points<\/h2>\n<ul>\n<li>Volume drivers allows to\n<ol>\n<li>Store volumes on remote hosts or cloud providers,<\/li>\n<li>Encrypt the contents of volumes.<\/li>\n<\/ol>\n<\/li>\n<li>Volumes does not increase the size of the containers using it.<\/li>\n<li>Volume\u2019s contents exist outside the lifecycle of a given container.<\/li>\n<\/ul>\n\n\n<pre class=\"wp-block-code\"><code>root@docker>docker volume ls\nDRIVER              VOLUME NAME\nlocal               c9ad85b13e22062f4eeefd9b65ae102c19856bea73622189f1a3393727c9b3e7<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Options to define volumes<\/h2>\n\n\n\n<p>One an define a volume using either <code>-v<\/code> or <code>--mount<\/code>. <\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><code>--mount<\/code><\/h3>\n\n\n\n<p>Option <code>--mount<\/code> is more explicit and allows one to avoid mistakes.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>It consists of multiple key-value pairs, separated by commas and each consisting of a <code>&lt;key&gt;=&lt;value&gt;<\/code> tuple.<\/p><\/blockquote>\n\n\n\n<p>The option <code>type<\/code> can be either of<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>volume<\/li><li>bind<\/li><li>tempfs<\/li><\/ol>\n\n\n\n<h3 class=\"wp-block-heading\"><code>-v<\/code> or <code>--volume<\/code><\/h3>\n\n\n\n<p>The order of information provided is very important for this option.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p><code>-v<\/code> or <code>--volume<\/code> Consists of three fields, separated by colon characters (<code>:<\/code>). <\/p><\/blockquote>\n\n\n\n<pre class=\"wp-block-code\"><code>root@docker>docker volume\n\nUsage:  docker volume COMMAND\n\nManage volumes\n\nCommands:\n  create      Create a volume\n  inspect     Display detailed information on one or more volumes\n  ls          List volumes\n  prune       Remove all unused local volumes\n  rm          Remove one or more volumes\n\nRun 'docker volume COMMAND --help' for more information on a command.<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Experiment time<\/h3>\n\n\n\n<p>You can simply create a volume without specifying any name<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>root@docker>docker volume create\n502f4ffc940ead1fc8cfaed65a9829b17aac322f9b4125b57e3a2e66f113a658<\/code><\/pre>\n\n\n\n<p><code>docker volume ls<\/code> will return the listing which will show the volume just created.<\/p>\n\n\n\n<p>You can give name to volumes too.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>root@docker>docker volume create my-volume\nmy-volume<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>root@docker>docker volume ls\nDRIVER              VOLUME NAME\nlocal               my-volume<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>root@docker>docker volume inspect my-volume\n&#91;\n    {\n        \"CreatedAt\": \"2020-07-31T07:06:07Z\",\n        \"Driver\": \"local\",\n        \"Labels\": {},\n        \"Mountpoint\": \"\/var\/lib\/docker\/volumes\/my-volume\/_data\",\n        \"Name\": \"my-volume\",\n        \"Options\": {},\n        \"Scope\": \"local\"\n    }\n]<\/code><\/pre>\n\n\n\n<p>deleting a volume is equally simple<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>root@docker>docker volume rm my-volume\nmy-volume<\/code><\/pre>\n\n\n\n<p>How about attaching a volume to a container?<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Step 1: Create a volume<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>root@docker>docker volume create myvol\nmyvol\nroot@docker>docker volume ls\nDRIVER              VOLUME NAME\nlocal               myvol<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Step 2: Refer to the volume<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>docker run -d --mount src=myvol,dst=\/app --name my_nginx nginx:latest<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>Unable to find image 'nginx:latest' locally\nlatest: Pulling from library\/nginx\n6ec8c9369e08: Pull complete \nd3cb09a117e5: Pull complete \n7ef2f1459687: Pull complete \ne4d1bf8c9482: Pull complete \n795301d236d7: Pull complete \nDigest: sha256:0e188877aa60537d1a1c6484b8c3929cfe09988145327ee47e8e91ddf6f76f5c\nStatus: Downloaded newer image for nginx:latest\ncee753ad7a48ccd4b1039391f7c10e8b710404472fce82e474319760e65bc89f<\/code><\/pre>\n\n\n\n<p>Inspecting the image can show the mount<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>root@docker>docker container inspect my_nginx<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>\"Mounts\": &#91;\n            {\n                \"Type\": \"volume\",\n                \"Name\": \"myvol\",\n                \"Source\": \"\/var\/lib\/docker\/volumes\/myvol\/_data\",\n                \"Destination\": \"\/app\",\n                \"Driver\": \"local\",\n                \"Mode\": \"z\",\n                \"RW\": true,\n                \"Propagation\": \"\"\n            }\n        ]<\/code><\/pre>\n\n\n\n<p>If you look carefully there is a <code>RW<\/code> property which can be defined as false if you use the option <code>ro<\/code>, <code>readonly<\/code> in the option.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>root@docker>ls -als \/var\/lib\/docker\/volumes\/myvol\/_data\ntotal 0\n0 drwxr-xr-x. 2 root root  6 Jul 31 07:12 .\n0 drwxr-xr-x. 3 root root 19 Jul 31 07:12 ..<\/code><\/pre>\n\n\n\n<p>Inspecting the volume <code>myvol<\/code><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>root@docker>docker inspect myvol\n&#91;\n    {\n        \"CreatedAt\": \"2020-07-31T07:12:00Z\",\n        \"Driver\": \"local\",\n        \"Labels\": {},\n        \"Mountpoint\": \"\/var\/lib\/docker\/volumes\/myvol\/_data\",\n        \"Name\": \"myvol\",\n        \"Options\": {},\n        \"Scope\": \"local\"\n    }\n]<\/code><\/pre>\n\n\n\n<p>The syntax in the <code>--mount<\/code> can be simplified by using <code>--volume<\/code><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>docker run -d -v myvol:\/app --name my_nginx nginx:latest<\/code><\/pre>\n\n\n\n<p>Please note if you are creating service you have to use the <code>--mount<\/code> option as the <code>-v<\/code> is not supported for <code>docker service create<\/code><\/p>\n\n\n\n<figure class=\"wp-block-pullquote\"><blockquote><p>None of the containers can share the data if you use the <code>local<\/code> volume driver<\/p><\/blockquote><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">References<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>https:\/\/docs.docker.com\/storage\/volumes\/#share-data-among-machines<\/li><\/ul>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Volumes are managed by Docker and is the preferred way of persisting, sharing data. Key Points Volume drivers allows to Store volumes on remote hosts or cloud providers, Encrypt the contents of volumes. Volumes does not increase the size of the containers using it. Volume\u2019s contents exist outside the lifecycle of a given container. Options [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":14,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"image","meta":{"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"footnotes":""},"categories":[86],"tags":[82,89],"class_list":["post-832","post","type-post","status-publish","format-image","has-post-thumbnail","hentry","category-docker","tag-docker","tag-volumes","post_format-post-format-image"],"_links":{"self":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/posts\/832","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/comments?post=832"}],"version-history":[{"count":0,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/posts\/832\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/media\/14"}],"wp:attachment":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/media?parent=832"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/categories?post=832"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/tags?post=832"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}