{"id":3158,"date":"2024-07-07T23:44:11","date_gmt":"2024-07-07T18:14:11","guid":{"rendered":"https:\/\/uitutorials.in\/wp\/?p=3158"},"modified":"2024-07-07T23:44:11","modified_gmt":"2024-07-07T18:14:11","slug":"docker-volume","status":"publish","type":"post","link":"https:\/\/uitutorials.in\/wp\/docker-volume\/","title":{"rendered":"Docker Volume"},"content":{"rendered":"<p>docker volume -h \/ help<br \/>\ndocker volume ls<br \/>\ndocker volume create <NAME> \/\/if name not given , by default it will generate random name<br \/>\ndocker volume inspect <NAME><br \/>\n\/\/output<br \/>\n[<br \/>\n    {<br \/>\n        &#8220;CreatedAt&#8221;: &#8220;2024-06-29T07:07:17Z&#8221;,<br \/>\n        &#8220;Driver&#8221;: &#8220;local&#8221;,<br \/>\n        &#8220;Labels&#8221;: null,<br \/>\n        &#8220;Mountpoint&#8221;: &#8220;\/var\/lib\/docker\/volumes\/vol-ubuntu\/_data&#8221;,<br \/>\n        &#8220;Name&#8221;: &#8220;vol-ubuntu&#8221;,<br \/>\n        &#8220;Options&#8221;: null,<br \/>\n        &#8220;Scope&#8221;: &#8220;local&#8221;<br \/>\n    }<br \/>\n]<br \/>\ndocker volume ls &#8211;filter dangling=true<br \/>\ndocker volume rm <NAME> (if volume associated with any container , it will not be removed first delete container then remove volume)<br \/>\ndocker volume prune (first delete container then remove volume)<br \/>\ndocker run -d &#8211;volume vol-ubuntu:\/tmp ubuntu(run ubuntu container with volume)<\/p>\n<p>docker run -d &#8211;volume vol-ubuntu:\/tmp ubuntu (create volume while run container)<br \/>\nTo update container : apt-get update<br \/>\ndocker volume ls &#8211;filter &#8220;dangling=true&#8221; (list unused volumes, if valume created but does not connected with any container)<br \/>\ndocker volume inspect vol-busybox<br \/>\ndocker volume rm vol-busybox<br \/>\ndocker container rm vol-busybox<\/p>\n<p>docker run -itd &#8211;name cont-ubuntu &#8211;volume vol-ubuntu:\/var\/log ubuntu<br \/>\ndocker volume ls<br \/>\n[ec2-user@abc~]$ sudo su<br \/>\n[root@abc]# cd \/var\/lib\/docker\/volumes\/ (volume stored location)<br \/>\n[root@abc volume]# ls<br \/>\nvol-ubuntu<br \/>\n[root@abc volume]# cd vol-ubuntu\/ =>ls<br \/>\n_data<br \/>\n[root@abc vol-ubuntu]# cd _data\/ =>ls<br \/>\napt btmp dpkg.log (apt &#8211; will be the stored volume)<br \/>\nthis location is mouted with container<\/p>\n<p>docker container inspect &#8211;format &#8220;{{json .Mounts}}&#8221; cont-ubuntu | python -m json.tool<br \/>\n(or)<br \/>\ndocker container inspect &#8211;format &#8220;{{json .Mounts}}&#8221; cont-ubuntu<br \/>\n\/\/output<br \/>\n[<br \/>\n  {<br \/>\n    &#8220;Type&#8221;: &#8220;volume&#8221;,<br \/>\n    &#8220;Name&#8221;: &#8220;vol-ubuntu&#8221;,<br \/>\n    &#8220;Source&#8221;: &#8220;\/var\/lib\/docker\/volumes\/vol-ubuntu\/_data&#8221;,<br \/>\n    &#8220;Destination&#8221;: &#8220;\/var\/log&#8221;,<br \/>\n    &#8220;Driver&#8221;: &#8220;local&#8221;,<br \/>\n    &#8220;Mode&#8221;: &#8220;z&#8221;,<br \/>\n    &#8220;RW&#8221;: true,<br \/>\n    &#8220;Propagation&#8221;: &#8220;&#8221;<br \/>\n  }<br \/>\n]<\/p>\n<p>To find the location where Docker stores volumes on Windows when using Docker Desktop with the WSL 2 backend, follow these steps:<\/p>\n<p>1.Open Windows File Explorer.<br \/>\n2.In the address bar, type \\\\wsl$ and press Enter.<br \/>\n3.You will see a list of your WSL distributions. Select your distribution (e.g., Ubuntu).<br \/>\n4. find the volume name : &#8220;vol-ubuntu&#8221;<\/p>\n<p>\\\\wsl.localhost\\docker-desktop\\mnt\\docker-desktop-disk\\data\\docker<br \/>\n\\\\wsl.localhost\\docker-desktop\\tmp\\docker-desktop-root\\mnt\\docker-desktop-disk\\data\\docker<br \/>\n\\\\wsl.localhost\\docker-desktop\\tmp\\docker-desktop-root\\mnt\\docker-desktop-disk\\data\\docker\\volumes\\vol-ubuntu\\_data\t<\/p>\n<p>we can see hard_disk volume in this location : C:\\Users\\Admin\\AppData\\Local\\Docker\\wsl\\disk<\/p>\n<p>Reade only volume :<br \/>\ndocker run -itd &#8211;name alpha-centor &#8211;mount source=vol-centos, destination=\/etc, readlonly centos (&#8220;readonly&#8221; keyword is important) (if we use &#8220;\\&#8221; this slash in middle of a command , it used for new line to continue command)<br \/>\n[ec2-user@abc~]docker exec -it alpha-centos bash<br \/>\n[root@abc]# cd \/etc<br \/>\n[root@etc]# ls<br \/>\n[root@etc etc]touch mytmp.conf<br \/>\n&#8216;can not touch mytmp.conf : Read-only file system&#8217;<\/p>\n<p>Volume Reflecting  &#8211; Vice Versa<br \/>\ndocker run -itd &#8211;name alpha-ubuntu -v myvolume:\/etc ubuntu<br \/>\n[ec2-user@abc~]$ sudo su<br \/>\n[root@etc]# cd \/var\/lib\/docker\/volumes\/ =>ls<br \/>\nmyvolume metadat.db<br \/>\n[root@etc volumes]# cd myvolume =>ls<br \/>\n_data<br \/>\n[root@etc volumes]# cd _data<br \/>\n[root@etc volumes]# touch mytemp.conf<br \/>\nNow go to check container , our file is created in container or not<br \/>\n[root@etc volumes]# docker exec -it alpha-ubuntu bash<br \/>\n[root@etc 4567]# ls \/etc<br \/>\nhere we can see mytemp.conf file<\/p>\n","protected":false},"excerpt":{"rendered":"<p>docker volume -h \/ help docker volume ls docker volume create \/\/if name not given , by default it will generate random name docker volume inspect \/\/output [ { &#8220;CreatedAt&#8221;: &#8220;2024-06-29T07:07:17Z&#8221;, &#8220;Driver&#8221;: &#8220;local&#8221;, &#8220;Labels&#8221;: null, &#8220;Mountpoint&#8221;: &#8220;\/var\/lib\/docker\/volumes\/vol-ubuntu\/_data&#8221;, &#8220;Name&#8221;: &#8220;vol-ubuntu&#8221;, &#8220;Options&#8221;: null, &#8220;Scope&#8221;: &#8220;local&#8221; } ] docker volume ls &#8211;filter dangling=true<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[189],"tags":[191,190],"class_list":["post-3158","post","type-post","status-publish","format-standard","hentry","category-devops","tag-devops","tag-docker","ct-col-2"],"_links":{"self":[{"href":"https:\/\/uitutorials.in\/wp\/wp-json\/wp\/v2\/posts\/3158","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/uitutorials.in\/wp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/uitutorials.in\/wp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/uitutorials.in\/wp\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/uitutorials.in\/wp\/wp-json\/wp\/v2\/comments?post=3158"}],"version-history":[{"count":1,"href":"https:\/\/uitutorials.in\/wp\/wp-json\/wp\/v2\/posts\/3158\/revisions"}],"predecessor-version":[{"id":3159,"href":"https:\/\/uitutorials.in\/wp\/wp-json\/wp\/v2\/posts\/3158\/revisions\/3159"}],"wp:attachment":[{"href":"https:\/\/uitutorials.in\/wp\/wp-json\/wp\/v2\/media?parent=3158"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/uitutorials.in\/wp\/wp-json\/wp\/v2\/categories?post=3158"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/uitutorials.in\/wp\/wp-json\/wp\/v2\/tags?post=3158"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}