Home Docker Explaining Docker Volumes With Examples

Explaining Docker Volumes With Examples

By editor
Published: Updated: 5.8K views

In the case of Docker, data will be stored inside the container. And when we remove the container data will be lost. In this guide, we will learn how to create docker volumes, how to attach it to the containers and how the same volume can be shared across two or more containers for storing the data.

If you don't know the docker basics yet, refer the following guide:

Why do we require volumes for Docker?

  • On deleting containers, volumes will not get deleted.
  • Attach or detach volume to the containers.
  • Share volumes (storage/data) among different containers.
  • Decoupling containers from storage.
  • Store volume on remote host or cloud.

We can have a logic division of the data as well as the container. We can preserve the data in a way that if we remove
the container then also our data will be present. So after destroying the container we can attach the volume to the any existing containers as well as a new containers.

Getting Started With Docker Volumes

The docker volume command will display the details of the volume commands by which we can create a volume, inspect about the detailed data of the volume, list the available volumes and remove the volumes.

# docker volume
docker volume command

Display "docker volume" command's help section

Creating Docker Volumes

The syntax to create a docker volume is:

docker volume create <volume_name>


# docker volume create devops_volume

The above command will create a docker volume named devops_volume.

create docker volume

create docker volume

Listing docker volumes

To list all created docker volumes, run:

# docker volume ls
list docker volumes

list docker volumes

Inspecting docker volumes

The docker volume inspect command will give you the details about creation of the volume, location, name and scope.


docker volume inspect <volume_name>

Let me show you the details of a docker volume named devops_volume.

# docker volume inspect devops_volume
inspect docker volumes

inspect docker volumes

Creating containers with the volume attached to them


docker container create --name <container_name> -it --mount source<volume_name>,target=/<folder_Name> <image_name>


# docker container create --name myBusyBox1 -it --mount source=devops_volume,target=/app busybox
create a container with the volume attached to it

create a container with the volume attached to it

To verify if the container has been created or not, use docker ps -a command:

docker ps command

"docker ps" command output

Starting containers

To start the newly created container, for example myBusyBox1, run:

# docker container start myBusyBox1
start containers

start docker containers

As you see in the above screenshot, the container myBusyBox1 has been created 8 minutes ago and started 6 seconds ago.

Copying files between containers from a shared volume

Let us create a scenario in which we will create the file in one container at the shared location and we will try to access the file from another container.

We already have created a container named myBusyBox1. Let us created another one, for example myBusyBox2.

create a container

create a container

Now we have two containers in running state, myBusyBox1 and myBusyBox2.

Let us connect to the first container and create a file inside the /app folder.

# docker exec -it myBusyBox1 sh

Above command will take you inside the container.

Then, create a new folder called devops inside /app folder and exit from the first container.

connect to first container

connect to first container

Now, copy a file, for example index.html, from local system to in the location /app/devops/ of the myBusyBox1 container.

# docker container cp index.html myBusyBox1:/app/Devops

Now connect to another second container i.e myBusyBox2 and verify that inside /app/devops folder the index.html file is present or not.

# docker exec -it myBusyBox2 sh
connect to second container

connect to second container

From the above screenshot it is confirmed that both containers are sharing the same volume.

We can check the Mountpoint location of the volume using docker volume inspect command like below:

# docker volume inspect devops_volume
docker inspect volume command

docker inspect volume command

As you see, the mountpoint location is /var/lib/docker/volumes/devops_volume/_data.

Let us go to the mountpoint location and see if the copied file is available or not.

docker volumes mountpoint

docker volumes mountpoint

In Mountpoint location we found the same file.

Delete docker volumes

The syntax to delete a volume is:

docker volume rm <volume_name>

Let us delete the volume called devops_volume.

# docker volume rm devops_volume
delete a docker volume

delete a docker volume

In above screenshot, we are trying to delete the volume but not able to delete because the volume is already in use. So firstly we need to kill the container and make volume free. And then we can delete the volume.

To do so, first remove all running containers using command:

# docker rm –f $(docker ps -aq)

And then remove the volume:

# docker volume rm devops_volume
delete docker volumes

delete docker volumes

Delete all volumes at once

Using docker rm command, we can remove one volume at a time. If we have multiple volumes and want to delete all volumes then we have to use prune command.

Let us create a few volumes:

create docker volumes

create docker volumes

Now delete all docker volumes at once using command:

# docker volume prune
delete all docker volumes at once

delete all docker volumes at once

See? We have deleted all volumes in one go.

Suggested read:

About the Author:

Dhruv Tiwari is a DevOps Engineer who loves automating things, working with Linux at scale and dream of the day when systems are smart enough to never need to login to a Linux box. Journey Of CI/CD from source code to code deployment to production.

Thanks for stopping by!

Help us to help you:

Have a Good day!!

You May Also Like

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. By using this site, we will assume that you're OK with it. Accept Read More