Introduction to Docker Volumes

Introduction to Docker Volumes

6 mins read151 Views Comment
Updated on Sep 25, 2022 00:07 IST

Many of us have used docker at some point of time. But, what if I ask you what are docker volumes? Many of us won’t be able to answer this. So, in this article, we will get an overview of docker volumes.


Understanding the concept of Docker Volumes is critical when using Docker. Here we will cover the following concepts associated with Docker Volumes: 

You can also explore: What is Docker?

What is a Docker Volume? 

The docker documentation describes volumes as the preferred mechanism for persisting data generated by and used by docker containers.  Simply put volumes are just folder locations that are designated to the docker containers for permanent data storage that can be used by one or more containers. 


The Need For Docker Volumes

In docker, we can create a volume, and we can then attach that volume(ie, folder) to a container. When we save data in the container, for instance, suppose we have a database container,  when we save data in that database we can then save it to the docker volume.  So the volume again is stored on the host machine, so which means when we destroy the container and spin up the container again we can still utilize the data that was created from the first container. Take a look at the below example for a better understanding. 

So the simple example here is that we have a database container. So if we were to go ahead and utilize this container without maybe connecting a volume to it, generally what would happen is that this database would store all the data in its virtual file system within the container. And as soon as we were to remove or delete the container the data would be destroyed too. Let’s also remember that containers are generated from images. As images are static, we cannot change or edit the image from the container.  Once a container is destroyed the data then is also destroyed.  



Anything we have in our container requires saving data in a stateful way. So stateful meaning the computer or program keeps track of the state or interaction. In our example, the database keeps track of the data. We interact with the database to store and update data in the database.  



The general application of a volume is that we can create a volume on the host(ie, /mnt/data in the above image). Then we can mount that volume onto the container. Generally, this means that the container has a link between the container and the folder on the host machine. We can utilize the data or we can save the data within the container which will further update the host. And then if we were to save data on the host in the folder that would also update what’s in the container. 

You can also explore: Top 10 Free Courses To Learn Docker In 2022

Of course, when we destroy the container, the mounted folder and the data reside on the host. Therefore when we return or we spin up the container again we can then go ahead and link that data that’s on the host back to the container and therefore we can return back to the previous state. 

Types of Docker Volumes 

Docker also provides the ability to provide encryption to your data. So the key here is that your data or your volume lives outside of the container. So we can think of three different types of volumes in docker: 

1. A Host Volume

The host volume lives on the docker host file system or anywhere you want to place it. We could have it on a remote host or cloud provider. And like all the other volume types, they’re all accessible from within the container. To set up a host volume we specify not only the path to the location where the volume is going to be held on the host.  We also specify where it’s going to be in the container. The syntax would look somewhat like the below: 

$ docker run -v /path/on/host:/path/in/container … 

You’ll notice some other common commands here, say for example docker run and then we use the -v flag here for volume to actually create this volume.  It is suggested to use a host volume when you need to know where to refer to the data. This will make it the easiest type of volume to use. These types of volumes are ideal for simple projects. 

2. An Anonymous Volume 

The location of the anonymous volume is managed by docker itself. So if you imagine you’ve got an anonymous volume, it means that it can be difficult to refer to that same volume for future connectivity. The syntax to create and run an anonymous volume would look somewhat like the below: 

$ docker run -v /path/in/container ... 

3. A Named Volume: 

In named volumes, there is a subtle difference. Here name volumes and anonymous volumes are similar in docker. Here the docker manages where they’re located, however, the named volumes as it suggests can be referred to by a specific name. This makes it easier for us to actually then utilize it in multiple containers. To implement them we first create a volume with the below command: 

$ docker volume create volume_name 

Then we run the newly created volume using the below command: 

$ docker run -v name:/path/in/container .. 

Useful Docker Volume Commands 

Now let’s take a look at some key docker volume commands. 

1. Create a Docker Volume: 

We can make use of the following command to create a docker Volume: 

$ docker volume create my_volume 



Note that above we create a named volume. If we do not specify a name, a random name is automatically assigned. 

$ docker volume create   



2. List all Docker Volumes 

To list all the volumes that Docker is aware of, we make use of the ls command as shown below: 

$ docker volume ls 



local   my_volume 

local   k11fb659f9b2f6c6fd7b2c796a47441fa77c8580a080e50fb0b1582c8f602r4o 

3. Inspect Docker Volumes 

We can make use of the inspect command to get the details of a specific docker volume as shown below: 

$ docker volume inspect my_volume 


    "CreatedAt": "2022-08-13T17:04:17Z", 
    "Driver": "local", 
    "Labels": null, 
    "Mountpoint": "/var/lib/docker/volumes/my_volume/_data", 
    "Name": "my_volume", 
    "Options": null, 
    "Scope": "local" 
Copy code

5.  Remove Specific Docker Volumes 

We make use of the rm  command to remove specific unused docker volumes as shown below: 

$ docker volume rm my_volume 


Are you sure you want to continue? [y/N] y 

Deleted Volumes: 


5.  Remove All Docker Volumes 

We make use of the prune command to remove all unused docker volumes as shown below: 

$ docker volume prune 


WARNING! This will remove all local volumes not used by at least one container. 

Are you sure you want to continue? [y/N] y 

Deleted Volumes: 


Note that the prune command will remove only the unused volumes from the container. 

You can also explore: Top Docker Interview Questions and Answers for 2022


In this article, we looked had a brief introduction to the following docker volume concepts: 

  • What is a Docker Volume? 
  • Why the need for docker volumes? 
  • Types of Docker volumes 
  • Key Docker commands associated with Volumes. 

Download this article as PDF to read offline

Download as PDF
About the Author

This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio


We use cookies to improve your experience. By continuing to browse the site, you agree to our Privacy Policy and Cookie Policy.