Working with Docker Volumes


To define Docker Volumes, they are file systems that can be mounted on Docker containers. They help in preserving the data and are independent of the container life cycle. One of the major advantages of Docker Volumes is that it allows the developers to backup their data and also allows easy sharing of file systems among Docker containers. We can easily mount a volume when we launch a Docker container. It is also possible to mount the same volume to different containers and this allows easy sharing of data between them and this can be easily achieved with the use of simple commands and flags.

In this article, we are going to discuss how to create, list, inspect, mount and delete docker volumes with the help of commands.

Creating a Docker Volume

You can create a Docker volume using the create command. On executing the command, Docker creates a particular directory for volume on the local machine. This directory is located in the path /var/lib/docker/volume.

The command for creating a Docker volume is −

sudo docker volume create <volume_name>

For example, if you want to create a volume with the name myVolume, you can do so using the following command.

sudo docker volume create myVolume.

Listing all the Docker Volumes

Now that you have created a docker volume, if you want to list all the existing Docker volumes, you can do so using the following command.

sudo docker volume list

Once you run the above command, a list will be displayed which would contain the driver name and the volume name of all the existing volumes.

Inspecting a Docker Volume

To inspect a particular Docker volume, you can use the Docker inspect command. It would list all the details regarding the Docker volume which would include the date of creation, mountpoint, driver name, name of the volume, etc.

The command for inspecting a Docker volume is −

sudo docker volume inspect <volume_name>

Mounting Docker Volumes

You can easily mount a Docker Volume to a Docker container using the --mount flag when you are running the Docker run command. You can also mount the same volume to multiple Docker containers and all the containers would have a shared access to the volume. The command for this is

sudo docker run −−mount source=<name of volume>,destination=<path of a directory in container> <image_name>

For example, if you want to mount a volume called myVolume to an ubuntu container at a location /usr/src/app/ inside the container, you can do so using the following command −

sudo docker run −it −−mount source=myVolume,destination=/usr/src/app/ ubuntu

This would open an ubuntu bash with the volume mounted in the specified location. You can verify this by going to the specified location inside the container.

Deleting a Docker Volume

In order to delete a docker volume, you need to ensure that the volume is not in use at that moment. If a container is running with the volume mounted in it, you would have to stop the container first before removing the mounted volume. After you have stopped the container, you can use the following command to remove the volume.

sudo docker rm <name of volume>

In order to delete all the volumes at once, you need to make sure that none of the volumes is currently in use and then use the following command.

sudo docker volume prune

Sharing a Docker Volume with multiple Docker Containers

Suppose that you want to share some files with multiple docker containers. In this case, you can put your files in a docker volume, mount that volume with multiple docker containers and get a shared access to that volume. Let’s discuss how to do that.

First create a volume using the volume create command and mount that volume to a particular docker container.

For example,

sudo docker volume create myVolume
sudo docker run −it −−name=container1 −−mount source=myVolume,destination=/app ubuntu

The above two commands would create a docker volume called myVolume and will mount this volume to a container called container1 of ubuntu image at a destination /app. And this would also open the bash of that particular container.

Inside the bash, you can see a directory called /app created there. Use the ls command to do so. Go to that directory and create a file and exit the bash. You can use the commands below.

ls
cd app
touch tutorialspoint.txt
exit

After that create a new container and mount the same volume in that container. You can use the below command.

sudo docker run −it −−name=container2 −−mount source=myVolume,destination=/app ubuntu

This would create a second container called container2 and mount the volume called myVolume at the location /app of the ubuntu image. You can verify the same by using the ls command. After going inside the /app directory, you would see the same tutorialspoint.txt file which you created in container1. If you make any changes inside the file, it would reflect in the other container as well.

To conclude, in this article, we saw all the basic commands to create, inspect, list, remove docker volumes. We also saw how to share a file among multiple docker containers by mounting the same volume to different docker containers.

Updated on: 27-Oct-2020

869 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements