Docker Volume

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
[
{
“CreatedAt”: “2024-06-29T07:07:17Z”,
“Driver”: “local”,
“Labels”: null,
“Mountpoint”: “/var/lib/docker/volumes/vol-ubuntu/_data”,
“Name”: “vol-ubuntu”,
“Options”: null,
“Scope”: “local”
}
]
docker volume ls –filter dangling=true
docker volume rm (if volume associated with any container , it will not be removed first delete container then remove volume)
docker volume prune (first delete container then remove volume)
docker run -d –volume vol-ubuntu:/tmp ubuntu(run ubuntu container with volume)

docker run -d –volume vol-ubuntu:/tmp ubuntu (create volume while run container)
To update container : apt-get update
docker volume ls –filter “dangling=true” (list unused volumes, if valume created but does not connected with any container)
docker volume inspect vol-busybox
docker volume rm vol-busybox
docker container rm vol-busybox

docker run -itd –name cont-ubuntu –volume vol-ubuntu:/var/log ubuntu
docker volume ls
[ec2-user@abc~]$ sudo su
[root@abc]# cd /var/lib/docker/volumes/ (volume stored location)
[root@abc volume]# ls
vol-ubuntu
[root@abc volume]# cd vol-ubuntu/ =>ls
_data
[root@abc vol-ubuntu]# cd _data/ =>ls
apt btmp dpkg.log (apt – will be the stored volume)
this location is mouted with container

docker container inspect –format “{{json .Mounts}}” cont-ubuntu | python -m json.tool
(or)
docker container inspect –format “{{json .Mounts}}” cont-ubuntu
//output
[
{
“Type”: “volume”,
“Name”: “vol-ubuntu”,
“Source”: “/var/lib/docker/volumes/vol-ubuntu/_data”,
“Destination”: “/var/log”,
“Driver”: “local”,
“Mode”: “z”,
“RW”: true,
“Propagation”: “”
}
]

To find the location where Docker stores volumes on Windows when using Docker Desktop with the WSL 2 backend, follow these steps:

1.Open Windows File Explorer.
2.In the address bar, type \\wsl$ and press Enter.
3.You will see a list of your WSL distributions. Select your distribution (e.g., Ubuntu).
4. find the volume name : “vol-ubuntu”

\\wsl.localhost\docker-desktop\mnt\docker-desktop-disk\data\docker
\\wsl.localhost\docker-desktop\tmp\docker-desktop-root\mnt\docker-desktop-disk\data\docker
\\wsl.localhost\docker-desktop\tmp\docker-desktop-root\mnt\docker-desktop-disk\data\docker\volumes\vol-ubuntu\_data

we can see hard_disk volume in this location : C:\Users\Admin\AppData\Local\Docker\wsl\disk

Reade only volume :
docker run -itd –name alpha-centor –mount source=vol-centos, destination=/etc, readlonly centos (“readonly” keyword is important) (if we use “\” this slash in middle of a command , it used for new line to continue command)
[ec2-user@abc~]docker exec -it alpha-centos bash
[root@abc]# cd /etc
[root@etc]# ls
[root@etc etc]touch mytmp.conf
‘can not touch mytmp.conf : Read-only file system’

Volume Reflecting – Vice Versa
docker run -itd –name alpha-ubuntu -v myvolume:/etc ubuntu
[ec2-user@abc~]$ sudo su
[root@etc]# cd /var/lib/docker/volumes/ =>ls
myvolume metadat.db
[root@etc volumes]# cd myvolume =>ls
_data
[root@etc volumes]# cd _data
[root@etc volumes]# touch mytemp.conf
Now go to check container , our file is created in container or not
[root@etc volumes]# docker exec -it alpha-ubuntu bash
[root@etc 4567]# ls /etc
here we can see mytemp.conf file

Leave a Reply

Your email address will not be published. Required fields are marked *