Lets dive into docker volumes

On the last post, We introduced you to docker, how to install Docker on both Windows and Linux, and we  discussed how you can use this at home to start packaging applications. On this article, we will discuss and learn what are Docker Volumes and why volumes are important for containerization. 

What are Docker Volumes and why is it important?

Docker containers are isolated environments that encapsulate all the dependencies, libraries, and configurations needed to run an application, making it easier to deploy and scale applications across different environments.

Docker volumes is a way for persisting data generated by your Docker containers. Docker volumes enable containers to share data with the host system and other containers, making it easier to manage, backup, and restore data. Docker volumes can be used to store configuration files, logs, databases, and other application data.

Docker volumes are important because they provide a way to manage data in a containerized environment. Without Docker volumes, data generated by containers would be lost when the container is deleted, stopped or restarted. Docker volumes also provide a way to share data between containers, making it easier to build complex applications that require multiple containers.

How do I use Docker Volumes?

Now that we understand Docker volumes and we know the importance, let’s learn how to use it.

Creating Docker Volumes

To create Docker volumes, you can use the docker volume create command to create a new volume. You can then use the docker run  command along with –mount or -v parameter to mount the volume to a container. For example, to create a new volume and mount it to a container, you can use the following command:

docker volume create mydata

docker run -v mydata:/app/data myimage
or
docker run --mount mydata:/app/data myimage

In this example, we created a new volume called mydata and mount it to the /app/data directory in the container. The myimage container can now read and write data to the mydata volume. Mounting volumes with the docker run command is composed of two parts divided by the colon, on this example, mydata: is the local mount point and :/app/data is the path of folder mapped on the container.

Managing Docker Volumes

Docker volume has different parameters to manage it. Here we will discuss some of the commands that can be used to manage the volumes on your system.

The best way to familiarize yourself with any commands is to use the help. In this case, we will be using docker docker volume --help this will display the different parameters that can be paired with docker volume.

To view docker volumes, we simply use docker volume ls. This command will show a list of volumes created. 

We use docker volume inspect, to view where the volume data is being stored on your localhost. 

We use docker volume rm or prune, to delete or remove the docker volumes. As suggested prune will completely remove deleted or unused volumes, while rm will just remove the volume.

Here is a breakdown of the actions of every command discussed and its output.


Final Remarks

Overall, Docker volumes are an essential feature of Docker that enable developers to persist data generated by containers and share data between containers. With Docker volumes, it’s easier to manage, backup, and restore data in a containerized environment making you one step closer to becoming a docker expert.

This can also be used if you need to manage any files within your container without having to access the container. An example would be modifying a static website hosted with nginx. You would attach a volume to store the hosted files and then access said files to get it modified or backed up. 

If you’d like to dive deeper into docker volumes, I recommend you to read through the Docker documentation about docker volumes.