How to configure Docker Volumes

Docker images are a series of read only layers. A container is an instantiation of those read only layers with a single read-write layer on the top. Any file changes that are made within a container are reflected as a copy of modified data from the read only layer. The version in the read-write layer hides the underlying file but does not remove it. When deleting a container, the read-write layer containing the changes are destroyed and gone forever. In order to persist these changes we use docker volumes. In this guide, you will learn how to configure Docker Volumes.

Docker engine manages data within the docker container using Docker Volumes. For instance let’s say that you are running an application that is generating data and it creates files or writes to a database and so on. Now even if the container is removed and in the future you launch another container, you would like that data to still be there. Until now all the files that we created in an image or a container are part of the Union filesystem. However, the data volume is part of the Docker host filesystem, and it simply gets mounted inside the container.

Advantages of Docker Volumes

Here are the few advantages if docker volumes.

  • Docker  volume is initialized when the container is created. By default, it is not deleted when the container is stopped.
  • Data volumes are designed to persist data, independent of the container’s lifecycle. Therefore, docker never automatically deletes volumes when you remove a container. nor it will “garbage collect” volumes that are no longer referenced by a container.
  • Data volumes can be shared across containers too, and can be mounted in read-only mode also.

Types of Docker Volume

There are two types of Docker Volume.

  • Named Volume
  • Bind Volume

Named Volume

A named volume can be created using ‘docker volume create’ command and it is mounted on default volume location /var/lib/docker/volumes.

$ docker volume create my-vol
$ docker run -d --name nginx -v my-vol:/app nginx

Or you can run following single command to create and mount docker volume

$ docker run -d --name nginx --mount source=my-vol2,target=/app nginx 

Bind Volume

In bind volume, You can mount external hard disks etc. Bind mounts may be stored anywhere on the host system. They usually start with ‘/’. A syntax of bind volume as follows,

$ docker run -d --name <container-name> -v <host-location>:<container-location> image

Here you can specify docker host mount location and container data location.

$ docker run -d --name nginx -v /root/html:/var/www/html/ nginx

Basic Docker Volumes commands

Here we consolidated few docker volumes commands,

Creating a new docker volume

Using below command, you can create a new docker volume.

$ docker volume create <volume_Name>

Listing Docker volumes

Docker volumes can be listed by following commands,

$ docker volume ls

Getting info about existing docker volume

You can get information regarding already created volumes such as creation date, mount point etc.

$ docker volume inspect <volume_Name>

Removing docker volumes

To remove unused docker volumes, you can use below command.

$ docker volume rm <volume_Name>

Removing all unused docker volumes

All unused docker volumes can be removed using a single command as follows

$ docker volume prune


In this guide, you learnt how to configure Docker Volumes. Stay tuned for more on docker. Thanks!!

Read Also :  How to configure Networking and Port Mapping in Docker

Share on:

Ravindra Kumar

Hi, This is Ravindra. I am founder of TheCodeCloud. I am AWS Certified Solutions Architect Associate & Certified in Oracle Cloud as well. I am DevOps and Data Science Enthusiast.

Recommended Reading..

1 thought on “How to configure Docker Volumes”

Leave a Comment