How Does Volume Work in Docker?

How Does Volume Work in Docker?
Techiio-author
Written by Nilima PaulFebruary 16, 2022
13 min read
Docker
3 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 3 VIEWS SHARE
Techiio-author
Nilima Paul

Technology Security Analyst

We, Will, discuss How Does Volume Work in Docker.

Introduction to Docker Volume:

Docker volume is a capacity instrument that is utilized for industrious information stockpiling created by Docker holders. Docker volumes are overseen by Docker itself. It is smarter to store information in a volume rather than putting away it in a holder's writable layer as it doesn't build the size of the compartment; additionally, compartments are vaporous, and that implies compartment lifetime is short, and compartments get obliterated, thus the information too. Docker volume can be imparted to more than one holder.

Syntax:

docker volume COMMAND

Commands of Docker Volume:

The following are the various orders of Docker Volume:

1. Make: It is utilized to make new volumes.

2. Ls: It is utilized to list every one of the volumes in a namespace.

3. Investigate: It is utilized to find out about any of the volumes.

4. Rm: It is utilized to eliminate any volume on the off chance that it is not generally needed.

5. Prune: It is utilized to eliminate every single unused volume.

Command:

docker volume --help

We can get more information about any command by using the ‘help’ option after the command as below:

Command:

docker volume COMMAND --help

Below is the example:

Command:

docker volume create --help

How Does Volume Work in Docker:

We utilize the '- v' or '- volume' choice to mount the Docker volume to independent compartments, and the '- mount' choice is utilized for swarm benefits as it were. Nonetheless, we can involve the '- mount' choice with the independent compartments also in Docker adaptation 17.06 and more current. The '- mount' is less difficult than '- I'm as it is more expressed and verbose. We should utilize the '- mount' banner while mounting the volume to the holder to determine a volume driver.

Let’s understand the syntax of each flag:

1. - v or - volume: It has three fields isolated by (:). The main field is the name of the volume and the subsequent field is the way of the document or organizer in the compartment where we will mount the volume, and the third one is discretionary that determines the entrance method of the mounted record. We should follow the field request accurately in this choice.

Below is the example:

Command:

-v my-vol:/etc/:ro

In the above model, 'my-vol' is the name of the volume, and '/and so forth/' is the envelope inside the holder, and access mode is perused as it were.

2. - mount: It has key-esteem sets, and it is isolated by commas.

Beneath keys are utilized in this choice:

  1. type: This key indicates the kind of the mount like a tie, volume, or tmpfs. Here, we are talking about Docker volume, so the kind of the mount will be volume.
  2. source: It determines the wellspring of the mount, and that implies any way on the host; notwithstanding, for named volume, we indicate the name of the volume. It very well may be determined as 'source' and 'src'.
  3. objective: It determines the way where we need to mount the record or catalog in the holder. It tends to be indicated as 'objective', 'DST', or 'target'.
  4. volume-select: This choice can be utilized on different occasions; it likewise takes key-esteem pair, which is comprised of a choice name and its worth.
  5. volume-driver: we can determine the volume driver utilizing this choice.

Examples to Implement Docker Volume:

Below are the examples of Docker Volume:

Situation: We will make a volume first and confirm it, and afterward we will take a look at the all insights regarding the recently made volume. We will then, at that point, mount this volume to a compartment and afterward make a test document in the holder where we mounted the volume. We will then, at that point, mount the similar volumes to various holders and check the test document around there to confirm it functioned true to form. How about we get everything rolling.

1. Create a new volume named ‘my-vol’ and list it as below.

docker volume create my-vol
docker volume ls

2. Check all details of this volume using the ‘inspect’ command as below:

Command:

docker volume inspect my-vol

Explanation: The 'Mountpoint' is where the documents or organizers going to store as a matter of fact. So assuming we peruse that area, there isn't anything at present. we will populate the information after mounting it to any compartment; then, at that point, assuming we peruse this area, we will get the records or organizers here that we have made at the mounting area in the holder.

3. Presently, mount this volume to a compartment utilizing the '- v's banner and associate with the holder utilizing the 'docker exec command, make another test record 'test.txt'. Pause and erase this holder and make another compartment and interface a similar volume to this holder. Once more, execute this new holder and check if the document is accessible or not in this recently made compartment as underneath:

Commands:

$docker run -d -v my-vol:/root --name my-cont alpine sleep 3600
$docker exec -it my-cont sh                  (Note: - we can use container ID as well.)
/  # cd /root
~ # echo “This is a test file” > test.txt
~ # cat test.txt
~ # exit
$docker container stop my-cont && docker container rm my-cont
$docker run -d --mount source=my-vol,target=/etc/ --name my-cont2 alpine sleep 3600
$docker exec -it my-cont2 sh
/  # cat /etc/test.txt
/  # exit

Explanation: In the above snapshot, we can see that the file is available even after the first container got destroyed.

4. If we want to see the above file without mounting it to any container as discussed in the explanation of point 2, then we have to browse the ‘Mountpoint’ of the volume as below:

Command:

sudo cat /var/lib/docker/volumes/my-vol/_data/test.txt

5. Let’s assume that this volume is no longer required and can be deleted; we can do so using the ‘rm’ command as below:

Command:

docker volume rm my-vol

docker container rm my -cont2

6. If multiple volumes need to be deleted, we use the ‘prune’ command to delete all unused volumes at once as below:

docker volume prune

Explanation: In the above example, we have created 3 new volumes and did not attach any container to them it so we can use the ‘prune’ command to delete all the three volumes at once

Advantages of Docker Volume:

  1. We can join volumes on the two sorts of holders, regardless of whether it is a Linux or Windows compartment.
  2. We can store volumes on independent hosts or any remote has or cloud suppliers, scramble the substance of volumes or add greater usefulness utilizing volume drivers.
  3. We can divide volumes between different compartments securely; reinforcement and relocation of volumes are additionally simple.

Conclusion:

Docker volumes are protected by any simple method for putting away the information perseveringly. We likewise have a tight spot mount for constant stockpiling; be that as it may, it has less usefulness in contrast with Docker volumes. We have examined named volumes, we likewise have unknown volumes that don't have a particular source, and it is eliminated once the holder got erased.

Docker
Docker Volume
DevOps
Docker secrets
Docker swarm
3 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 3 VIEWS SHARE
Was this blog helpful?
techiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plan
You must be Logged in to comment
Code Block
Techiio-logo

Techiio is on the journey to build an ocean of technical knowledge, scouring the emerging stars in process and proffering them to the corporate world.

Follow us on:

Subscribe to get latest updates

You can unsubscribe anytime from getting updates from us
Developed and maintained by Wikiance
Developed and maintained by Wikiance