Understanding Docker  Volumes for DevOps Engineers.

Understanding Docker Volumes for DevOps Engineers.

Docker Volumes

Hi Friends,

In the last blog, we discuss docker architecture. In this blog, we are moving one step ahead toward docker containers. What if the container dies? If the container dies then can we recover the data? , How can we share the data between containers? Yes, these are all we are going to discuss in this blog.

  • What are Docker Volumes?

    Docker volume is used to store data of a container and share data between containers and the host. So if the container dies or crashes we will not lose our data. We can run a fresh container and can attach the volume to it. In simple words, volume is used to share the data between containers as well as between containers to host. Volumes are easier to back up.

    Let's discuss the types of volumes available.

    Types of Volumes :

    1. anonymous volumes

      Anonymous volumes are created when we haven't mentioned our directory name but still, it will create a directory anonymously and map it to a running container. Let's understand with an example :

      Suppose we run the below command.

      $docker run -v /demo1

      The above command will run the container and creates a demo1 directory under it. It will also create a directory on the host path: /var/lib/volumes/random_hash/_data

      here we haven't mentioned directory name of a volume so it creates a volume random_hash and maps with the container demo1 directory. So if the container is lost still we will have an anonymous volume.

    2. Named volumes

      We can create Named volumes by using the docker run command.

      E.g.

      docker run -v mydata_vol:/demo1

      Now we can easily find out our volumes by name.

    3. host volumes or bind volumes

      We can create host volumes by using the docker run command but here we are giving the path of a docker host directory and mapping with the container data directory.

      docker run -v /opt/mydata:/demo1

      So here also we can persist our data if the container doesn't exist or the container dies.

Docker Volumes Commands

  • How to see the docker volume list?

    $docker volume ls

  • How to create volume?

  • How to See detailed information about volumes?

  • How to use a volume with a container?

root@ip-172-31-95-7:~# docker run -d --name cont1 -v my_vol:/usr/share/ngnix/html/html/ -p 8080:80 nginx

  • How to remove all unused volumes?

      root@ip-172-31-95-7:~#docker volume prune
    
  • How to remove one or more volumes?

       root@ip-172-31-95-7:docker volume rm cont2
    

Thank you for reading this blog.Happy Learning!!!!!

You can follow me on LinkedIn for my daily updates:- linkedin.com/in/parimal-pradhan-b62021168

Great initiative by the #trainwithshubham community. Thank you Shubham Londhe

Did you find this article valuable?

Support Parimal Pradhan by becoming a sponsor. Any amount is appreciated!