Docker Volume Tutorial | Docker Documentation

Docker Volume Tutorial | Docker Documentation

As we know, docker runs the application into the containers. So, when the container terminates, data within the container is eliminated and goes away with the containers. Therefore, when the new container comes up it has no data to work on and it's completely empty.

To overcome this scenario and make the application data persistent, we can come up with a solution of binding the docker data onto the host machine. That is we can call volume mounting or data mounting. In simple terms, volume mounting means binding of files and data of the containers to the host machine.

This concept can be expressed pictorially as mentioned below. And will explain every terms in depth.

docker volume tutorial

As shown in the command below, files within the container are binded to the host machine using tag "-v" alternatively you can also use the "--mount" command.

sudo docker run --name my-nginx --restart=always -v /usr/share/nginx/html/:/usr/share/nginx/html/ -v /etc/nginx/:/etc/nginx/ -p 80:80 -d nginx:latest

from the above command, we can say that

  • files inside /usr/share/nginx/html/ in container are binded with files inside /usr/share/nginx/html/ in host machine.
  • files inside /etc/nginx/ in container are binded with files inside /etc/nginx/ in host machine.

command explanation: "-v host machine-path:inside-container-path"

Docker Volumes

Volumes are put away in an aspect of the host document framework which is overseen by Docker (/var/lib/docker/volumes/on Linux).

Uses of Docker Volume:

  • To keep information around when a container is eliminated
  • To share information between the hosts record framework and the Docker container
  • To share data with other Docker containers

Docker Volume commands:

To create a Docker volume

           docker volume create <vol_name>

Display detailed information on one or more volumes

           docker volume inspect <vol_name>

To List docker volumes

           docker volume ls

Remove all unused local docker volumes

           docker volume prune

To Remove volume

           docker volume rm <volume_name>

Docker Temporary File system(Docker tmpfs)

tmpfs stands for Docker temporary file system in which the data is temporarily stored or bound on to the host machine and it is not used for persistent volumes.

 

Leave a Reply

Your email address will not be published. Required fields are marked *