Docker volumes

Hello everyone, In this section I am going to cover the basics of docker volumes.

Let’s start with why it is needed and go through several examples and understand the use cases.

Why we need volumes?

As you know once the container is completed its task it automatically shutdown or if there is some error container will shutdown automatically. Think if we run a mysql container and we cannot simply let container to shutdown and if it shutdown and removed we lost all the data. Then the volumes come to solve this issue.

Volume is an external file or directory which we can plug in to the container and even container dies the files are persistently save in the volume.

Types of volumes

There are three types of volumes in docker as follows.

  1. Host volumes
  2. Anonymous volumes
  3. Named volumes

Host volumes are created in the host machine and named and anonymous volumes are created and handled by docker itself. Let’s go through one by one and understand the use cases.

Host volumes

Host volumes are the ones which we link our local files to the container. Let’s make a directory and file in our local machine and link it to the host as follows.

// Make the directory locally
mkdir data && cd data
// Create a file hello.txt and add a string
echo "Hi There" >> hello.txt

To link the host volume you have to follow following conventions.

docker run -v <source>:<destination> image 

The source is the host file system location and the destination is the container location. Note that if the destination folder is not in the container it will automatically creates it. I will use alpine as the file system. Let’s continue the tutorial.

// Link the local file to the container
docker run -v $(PWD):/data alpine
  • Note PWD is to get the current directory and it might not work in windows powershell.

You can do it either start and exec the running container or override the startup command when run it. Let’s use the second approach.

docker run -it -v $(PWD):/data alpine sh

Then it will appear a window which will take user inputs.

// Check the file system.

The output is as follows.

bin    dev    home   media  opt    root   sbin   sys    usr
data etc lib mnt proc run srv tmp var

Let’s go and inspect whether hello.txt is file is there.

// Go to the directory
cd data
// Inspect hello.txt
cat hello.txt

It will output,

Hi hello

Note: When you change either hello.txt file in the local machine or the container it will appear in the both places as it is connected. Following are some use cases.

  • It is very useful in development as we can locally update the code and it is reflected in the container.
  • It can be used to store database data in local machine.

Anonymous volumes

Anonymous volumes are maintained by the docker itself. These volumes are given a hash value as the name. Following is the way of using an anonymous volume. This approach is not much used because of named volumes.

docker run -v <destination> <image>

Named volumes

named volumes are also similar as the anonymous volumes which are maintained by docker but the difference is user can assign a name as follows.

docker run -v <name>:<destination> <image>

Example is as follows,

docker run -v files:/files alpine

Then it will create a volume named files if not exists in docker.

The common use case is these named volumes can be plugged into multiple containers as host volumes. It is similar as host volumes but the difference is it is maintained by docker.

Docker volume commands

  • docker volume ls — To get all the volumes
  • docker volume inspect <volume> — Get details of a volume
  • docker volume rm <volume> — Remove a docker volume

Hope you get a better understanding about Docker volumes.

If you have found this helpful please hit that 👏 and share it on social media :)



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store