{"id":5437,"date":"2022-10-03T14:26:43","date_gmt":"2022-10-03T14:26:43","guid":{"rendered":"https:\/\/www.devopsschool.com\/blog\/?p=5437"},"modified":"2025-07-12T05:40:15","modified_gmt":"2025-07-12T05:40:15","slug":"understand-docker-volume-from-beginner-to-deep-dive-level","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/understand-docker-volume-from-beginner-to-deep-dive-level\/","title":{"rendered":"Docker Tutorials: Volume &#8211; Deep Dive"},"content":{"rendered":"<p>In order to understand what a Docker volume is, we first need to be clear about how the filesystem normally works in Docker. Docker images are stored as series of<strong> read-only layers<\/strong>. When we start a container, Docker takes the read-only image and adds a <strong>read-write layer<\/strong> on top. If the running container modifies an existing file, the file is copied out of the underlying read-only layer and into the top-most read-write layer where the changes are applied. The version in the read-write layer hides the underlying file, but does not destroy it \u2014 it still exists in the underlying layer.<\/p>\n<p>When a Docker container is deleted, relaunching the image will start a fresh container without any of the changes made in the previously running container \u2014 those changes are lost. Docker calls this combination of read-only layers with a read-write layer on top a <strong>Union File System<\/strong>.<\/p>\n<p><a href=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2018\/10\/docker-image-containers-layers.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-5440\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2018\/10\/docker-image-containers-layers.png\" alt=\"\" width=\"421\" height=\"371\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2018\/10\/docker-image-containers-layers.png 421w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2018\/10\/docker-image-containers-layers-300x264.png 300w\" sizes=\"auto, (max-width: 421px) 100vw, 421px\" \/><\/a><\/p>\n<p><strong>Docker has two options for containers to store files in the host machine<\/strong>, so that the files are persisted even after the container stops: volumes, and bind mounts. If you\u2019re running Docker on Linux you can also use a tmpfs mount.<\/p>\n<p>An easy way to visualize the difference among volumes, bind mounts, and tmpfs mounts is to think about where the data lives on the Docker host.<\/p>\n<p><a href=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2018\/10\/types-of-docker-volume-mounts.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-5439\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2018\/10\/types-of-docker-volume-mounts.png\" alt=\"\" width=\"501\" height=\"255\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2018\/10\/types-of-docker-volume-mounts.png 501w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2018\/10\/types-of-docker-volume-mounts-300x153.png 300w\" sizes=\"auto, (max-width: 501px) 100vw, 501px\" \/><\/a><\/p>\n<ol>\n<li><strong>Volumes<\/strong> &#8211; Volumes are stored in a part of the host filesystem which is managed by Docker (\/var\/lib\/docker\/volumes\/ on Linux). Volumes are the best way to persist data in Docker.<\/li>\n<li><strong>Bind mounts<\/strong> &#8211; Bind mounts may be stored anywhere on the host system. They may even be important system files or directories. Non-Docker processes on the Docker host or a Docker container can modify them at any time.<\/li>\n<li><strong>tmpfs<\/strong> &#8211; tmpfs mounts are stored in the host system\u2019s memory only, and are never written to the host system\u2019s filesystem.<\/li>\n<\/ol>\n<p><strong>What are docker volumes?<\/strong><br>Docker volumes are the preferred mechanism for persisting data generated by and used by Docker Containers. These are use of Volume&#8230;<\/p>\n<ol>\n<li>Decoupling container from storage<\/li>\n<li>Share volume (storage\/data) among different containers<\/li>\n<li>Attach Volume to a container<\/li>\n<li>On Deleting container volume does not delete<\/li>\n<\/ol>\n\n\n<p><strong>Commands Collection<\/strong><\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-comment\"># How to create docker volume?<\/span>\n$ docker volume create myvol1\n\n<span class=\"hljs-comment\"># How to list the docker volume?<\/span>\n$ docker volume ls\n\n<span class=\"hljs-comment\"># How to inspect docker volume?<\/span>\n$ docker volume inspect vol-name\n\n<span class=\"hljs-comment\"># How to delete docker volume?<\/span>\n$ docker volume rm vol-name\n\n<span class=\"hljs-comment\"># How to attach Volume to a containers? or<\/span>\n<span class=\"hljs-comment\"># How to share volume among containers?<\/span>\n$ docker run --name jenkins1 -v myvol1:\/<span class=\"hljs-keyword\">var<\/span>\/jenkins_home -p <span class=\"hljs-number\">8080<\/span>:<span class=\"hljs-number\">8080<\/span> -p <span class=\"hljs-number\">50000<\/span>:<span class=\"hljs-number\">50000<\/span> jenkins\n$ docker run --name jenkins2 -v myvol1:\/<span class=\"hljs-keyword\">var<\/span>\/jenkins_home -p <span class=\"hljs-number\">8085<\/span>:<span class=\"hljs-number\">8080<\/span> -p <span class=\"hljs-number\">50005<\/span>:<span class=\"hljs-number\">50000<\/span> jenkins&#91;\n\n<span class=\"hljs-comment\"># How to use Bind mounts storage in Docker Volume?<\/span>\n$ docker run --name jenkins2 -v \/opt\/jenkins:\/<span class=\"hljs-keyword\">var<\/span>\/jenkins_home -p <span class=\"hljs-number\">8090<\/span>:<span class=\"hljs-number\">8080<\/span> -p <span class=\"hljs-number\">50010<\/span>:<span class=\"hljs-number\">50000<\/span> jenkins\n$ docker run --name jenkins2 -v \/opt\/jenkins:\/<span class=\"hljs-keyword\">var<\/span>\/jenkins_home -p <span class=\"hljs-number\">8095<\/span>:<span class=\"hljs-number\">8080<\/span> -p <span class=\"hljs-number\">50015<\/span>:<span class=\"hljs-number\">50000<\/span> jenkins\n\n<span class=\"hljs-comment\"># Difference between the -v or --mount flag in Docker?<\/span>\nOriginally, the -v <span class=\"hljs-keyword\">or<\/span> --volume flag was used <span class=\"hljs-keyword\">for<\/span> standalone containers <span class=\"hljs-keyword\">and<\/span> the --mount flag was used <span class=\"hljs-keyword\">for<\/span> swarm services. However, starting with Docker <span class=\"hljs-number\">17.06<\/span>, you can also <span class=\"hljs-keyword\">use<\/span> --<span class=\"hljs-title\">mount<\/span> <span class=\"hljs-title\">with<\/span> <span class=\"hljs-title\">standalone<\/span> <span class=\"hljs-title\">containers<\/span>. <span class=\"hljs-title\">In<\/span> <span class=\"hljs-title\">general<\/span>, --<span class=\"hljs-title\">mount<\/span> <span class=\"hljs-title\">is<\/span> <span class=\"hljs-title\">more<\/span> <span class=\"hljs-title\">explicit<\/span> <span class=\"hljs-title\">and<\/span> <span class=\"hljs-title\">verbose<\/span>.\n\n# <span class=\"hljs-title\">The<\/span> <span class=\"hljs-title\">biggest<\/span> <span class=\"hljs-title\">difference<\/span> <span class=\"hljs-title\">is<\/span> <span class=\"hljs-title\">that<\/span> <span class=\"hljs-title\">the<\/span> -<span class=\"hljs-title\">v<\/span> <span class=\"hljs-title\">syntax<\/span> <span class=\"hljs-title\">combines<\/span> <span class=\"hljs-title\">all<\/span> <span class=\"hljs-title\">the<\/span> <span class=\"hljs-title\">options<\/span> <span class=\"hljs-title\">together<\/span> <span class=\"hljs-title\">in<\/span> <span class=\"hljs-title\">one<\/span> <span class=\"hljs-title\">field<\/span>, <span class=\"hljs-title\">while<\/span> <span class=\"hljs-title\">the<\/span> --<span class=\"hljs-title\">mount<\/span> <span class=\"hljs-title\">syntax<\/span> <span class=\"hljs-title\">separates<\/span> <span class=\"hljs-title\">them<\/span>.\n$ <span class=\"hljs-title\">docker<\/span> <span class=\"hljs-title\">run<\/span> -<span class=\"hljs-title\">d<\/span> --<span class=\"hljs-title\">name<\/span> <span class=\"hljs-title\">devtest<\/span> --<span class=\"hljs-title\">mount<\/span> <span class=\"hljs-title\">source<\/span>=<span class=\"hljs-title\">myvol2<\/span>,<span class=\"hljs-title\">target<\/span>=\/<span class=\"hljs-title\">app<\/span> <span class=\"hljs-title\">nginx<\/span>:<span class=\"hljs-title\">latest<\/span>\n$ <span class=\"hljs-title\">docker<\/span> <span class=\"hljs-title\">run<\/span> -<span class=\"hljs-title\">d<\/span> --<span class=\"hljs-title\">name<\/span> <span class=\"hljs-title\">devtest<\/span> -<span class=\"hljs-title\">v<\/span> <span class=\"hljs-title\">myvol2<\/span>:\/<span class=\"hljs-title\">app<\/span> <span class=\"hljs-title\">nginx<\/span>:<span class=\"hljs-title\">latest<\/span>\n\n# <span class=\"hljs-title\">This<\/span> <span class=\"hljs-title\">commands<\/span> <span class=\"hljs-title\">will<\/span> <span class=\"hljs-title\">not<\/span> <span class=\"hljs-title\">work<\/span> <span class=\"hljs-title\">as<\/span> <span class=\"hljs-title\">its<\/span> --<span class=\"hljs-title\">mount<\/span> <span class=\"hljs-title\">require<\/span> <span class=\"hljs-title\">only<\/span> <span class=\"hljs-title\">volume<\/span> <span class=\"hljs-title\">type<\/span>. <span class=\"hljs-title\">but<\/span> <span class=\"hljs-title\">not<\/span> <span class=\"hljs-title\">the<\/span> <span class=\"hljs-title\">mounts<\/span>.\n<span class=\"hljs-title\">docker<\/span> <span class=\"hljs-title\">run<\/span> -<span class=\"hljs-title\">d<\/span> --<span class=\"hljs-title\">name<\/span> <span class=\"hljs-title\">devtest<\/span> --<span class=\"hljs-title\">mount<\/span> <span class=\"hljs-title\">source<\/span>=\/<span class=\"hljs-title\">opt<\/span>\/<span class=\"hljs-title\">backup1<\/span>,<span class=\"hljs-title\">target<\/span>=\/<span class=\"hljs-title\">var<\/span>\/<span class=\"hljs-title\">jenkins_home<\/span> <span class=\"hljs-title\">jenkins<\/span>\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n<div class=\"epyt-gallery\" data-currpage=\"1\" id=\"epyt_gallery_87285\"><figure class=\"wp-block-embed wp-block-embed-youtube is-type-video is-provider-youtube epyt-figure\"><div class=\"wp-block-embed__wrapper\"><iframe loading=\"lazy\"  id=\"_ytid_19331\"  width=\"760\" height=\"427\"  data-origwidth=\"760\" data-origheight=\"427\" src=\"https:\/\/www.youtube.com\/embed\/?enablejsapi=1&#038;autoplay=0&#038;cc_load_policy=0&#038;cc_lang_pref=&#038;iv_load_policy=1&#038;loop=0&#038;rel=1&#038;fs=1&#038;playsinline=0&#038;autohide=2&#038;theme=dark&#038;color=red&#038;controls=1&#038;disablekb=0&#038;\" class=\"__youtube_prefs__  no-lazyload\" title=\"YouTube player\"  data-epytgalleryid=\"epyt_gallery_87285\"  allow=\"fullscreen; accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen data-no-lazy=\"1\" data-skipgform_ajax_framebjll=\"\"><\/iframe><\/div><\/figure><div class=\"epyt-gallery-list\"><div>Sorry, there was a YouTube error.<\/div><\/div><\/div>","protected":false},"excerpt":{"rendered":"<p>In order to understand what a Docker volume is, we first need to be clear about how the filesystem normally works in Docker. Docker images are stored as series of read-only layers. When we start a container, Docker takes the read-only image and adds a read-write layer on top. If the running container modifies an&#8230;<\/p>\n","protected":false},"author":1,"featured_media":8017,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"_kad_post_classname":"","_joinchat":[],"footnotes":""},"categories":[4862],"tags":[1243,1194,567,213,5525],"class_list":["post-5437","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-docker","tag-beginner","tag-container","tag-docker","tag-tutorial","tag-volume"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/5437","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/comments?post=5437"}],"version-history":[{"count":11,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/5437\/revisions"}],"predecessor-version":[{"id":31447,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/5437\/revisions\/31447"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media\/8017"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=5437"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=5437"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=5437"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}