Saiyam Pathak avatar
By Saiyam Pathak
Director of Technical Evangelism


With Civo Academy, learn how to create a Docker volume or how volumes in general work within docker.



Hi. In this lecture, we'll go through Docker volumes. Now, what happens when we remove the container or restart it? What happens to the data? So, the data is not persistent and is limited to the container scope. So, when the container is restarted, stopped, and removed, the data within the container also gets lost. And when we try to spin a new container, it'll be a new container with a new state. But we don't want that. In most of the scenarios, we want our data to be persistent. So, to persist the data, maybe it's a database or a stateful application that we need the data. So, we use docker volumes.

How to create a docker volume

Let's see how to create a docker volume or how volumes in general work within docker. So, first, let's use the docker run command, docker, run -it -v. So -v will be the command to specify the docker volume. Now, we'll give a host path which will be /home. And then, we specify the path within the container that we want to mount to the host file system, which will be /temp of the busybox container. Hence, the whole command will be docker, run -it -v /home:/temp/ busybox. When we run it, we can see that it pulls the latest image. It has pulled the latest image, and we are inside the busybox shell. So, let's go to the temp directory. Let's create a test file by using the command tmp # touch test. If we use the command ls, we can see the file. Now, let's exit it. For that, use the command exit. Now when we have exited, docker itself will persist the data inside the host path that we have given and whatever is there. The host path will also be visible in the container. So let's go to /home. Let's do ls, and we can see the test file has already been created over there. Now, what happens is, this is called host mounting inside the docker container. The next portion is docker volumes itself. So these are, whatever I have shown you right now, there's the host path, and docker does not manage these.

Now, let's create another volume. And this time, we give a volume name. And we will mount another directory called usr/share of the busybox container. So, this command will be docker run -it -v demo:/usr/share busybox. Now, what happens is, I again come inside the busybox shell. And if I go there, I don't have anything else. Hence, I will create a file by using the command touch demo, and I have a file. Let's exit the container. Now docker will automatically create a demo volume, and docker will manage this volume. So let's create a screen. You can use the command docker volume ls to view the volumes. You can see the demo is already created. So this is called named volumes. Now, let's see where it is created. Now use the command cd var/lib/docker/volumes, and you can see that the demo file is there. If I go to the demo folder and go to _data folder, I will see the demo file is already listed here. So this is how docker creates volume.

If we do not give anything in the docker run command with -v, we specify the path we want to get mounted inside the container. So, we provide another path, /home of busybox. The entire command will be docker run -it -v /home busybox. Let's go to the home directory with cd /home and list it with ls. You can see that we don't have anything. Let's create a file test2 with the command touch test2. Check it with ls. Let's exit it. Now, docker will automatically build a volume with a random hash. So, if we use the command docker volume ls, we can see it is an anonymous volume that docker has already created. So the data is still getting persisted, but it isn't an anonymous volume because it is a randomly generated hash with which it has made the volume name.

If you go to var/lib/docker/volumes again, we can see we have a demo folder and the randomly generated new anonymous volume. So, this is how we can create docker volumes. And this is how we can persist data on the host file system or a volume managed by docker. Usually, the named volumes are most widely used. So, that's it for this lecture. Thank you.

Civo course complete badge

You've successfully completed our course on Before Kubernetes

We hope you enjoyed learning and encourage you to check out our other courses to further expand your knowledge on Kubernetes