Some time ago, we talked about how to create and use Docker networks. Now we will focus more on data. If you are beginning your Docker journey and wondering how to use Docker volumes, you are in the right place. In this article, I will guide you from the basic concepts to more advanced options using simple and casual language. Let us dive into the fascinating world of Docker volumes!
Before going deeper, it is important to understand what Docker volumes are. In simple terms, volumes are a way to persist and share data between containers and the underlying host. You can think of them as special folders that reside outside the container but can be accessed and used by containers.
Now, how can we use Docker volumes? First, let us explore how to create a volume and mount it in a container. We will use the docker volume create command to create a new volume. For example:
$ docker volume create mi_volumen
Now that we have created our volume called “mi_volumen”, we can mount it in a container using the -v or --volume option when running the container. Let us look at a practical example:
$ docker run -d -v mi_volumen:/ruta/del/volumen mi_contenedor
In this example, -v mi_volumen:/ruta/del/volumen specifies that we want to mount the “mi_volumen” volume in the “/ruta/del/volumen” directory inside the “mi_contenedor” container. Now, any change made to that directory inside the container will be reflected in the volume and vice versa.
In addition to mounting volumes, Docker also provides advanced options for working with them. One of these options is the ability to share volumes between containers. This is useful when you want several containers to access the same data. You can do this using the --volumes-from option when running a container. Let us look at an example:
$ docker run -d --volumes-from=mi_contenedor_origen mi_otro_contenedor
In this case, --volumes-from=mi_contenedor_origen indicates that we want to share the volumes mounted in “mi_contenedor_origen” with “mi_otro_contenedor”. This allows both containers to access and modify the data in the shared volume.
Finally, I want to mention another interesting option: anonymous volumes. Docker allows you to create anonymous volumes that do not have an explicit name. These volumes are useful when you only need a temporary place to store data. You can create an anonymous volume using the -v or --volume option without specifying a name:
$ docker run -d -v /ruta/del/volumen mi_contenedor
In this case, Docker will automatically create an anonymous volume and mount it at “/ruta/del/volumen” inside the container. Keep in mind that anonymous volumes do not persist after the container is stopped and removed.
Congratulations! You now have a basic understanding of how to use Docker volumes. Remember that volumes allow you to persist and share data between containers and the underlying host. You can create, mount, and share volumes using specific Docker commands and options.
I hope this article has been useful and that you feel more comfortable working with Docker volumes! Keep experimenting and exploring the many possibilities Docker offers for facilitating the development and management of containerized applications. Have fun learning and building!

