Types Of Volumes In Docker

Muhammad Basir Baig
4 min readFeb 17, 2021
Source

Note: This articles assumes that you have basic knowledge of how docker works and what it is…

Overview:

When ever we run a container based on an image e.g. (running a database in container) and have generated some data. After that if we remove that container the data will no longer be accessible to us the data gets removed with the container. And what if we want to preserve that data generated by running containers. So here’s where Docker Volumes comes in…

Docker volumes are a filesystem used in docker to persist data of a container independent of its life cycle. It helps us to persist or store either a file or a directory so that we can share it across multiple container or to create new containers from it.

Types of Volumes

We can store data with docker on our host machine in two ways.

  1. Volumes
  2. Bind Mounts

1. Volumes:

Volumes are usually managed by docker on our host machine on linux at this
path (/var/lib/docker/volumes/).We can create two types of volume in docker. Further we have two types of volumes in docker.

  1. Named Volumes
  2. Unnamed Volumes (Anonymous volumes)

Named Volumes

Named volumes cannot be mentioned in docker image. Named volume does not get remove on removal of a container. Named volumes can be used with multiple containers. We can create named volumes like this:

then we can use this volume while running a container like this

if the <volumeName> you have mentioned exists, docker will use that volume, if it doesn't not exist then docker will create a volume based on this name and will run your container with it.

Unnamed Volumes

Unnamed volumes can be mentioned in docker image and are good to use if the data is only going to be used by current container only. Whenever the container gets removed unnamed volumes associated with will also gets removed. We can create unnamed volumes while run a container like this

2. Bind Mounts

Bind mounts can be stored anywhere on the host system and are editable and accessible. When we use a bind mount a file or directory on the host machine is mounted into a container. The file or directory is mentioned by its full path on host machine. The file or directory which we are using to mount into container does not have to be already existed. It will be created by docker if it doesn't exists. We can use bind mount while running a container like this:

running this command will create a volume. And any changes will be reflected into this host file or directory as per operation.

Docker Volume Useful Commands

To create a named volume

To list down the all volumes on host system

To remove a volume

In this article, we learned about Docker Volumes. Hope you have learned something new today and if you liked this article please make a clap and follow me for more content on Medium. Thanks for reading 🙏

--

--