Docker Volume

Suppose you have a container running in your environment and somehow the container gets killed or removed accidentally. So the data inside of the container will also be deleted or will also be lost. So for avoiding the data loss docker came up with the solution of volumes in it. You can mount your volume to the directory inside the container that is generating some data inside your container for data recovery.

Let us take an example, suppose you have an elastic search container running in your system and it generates data inside the container in path /usr/share/elasticsearch /data, so we can mount volume to it so that our data stays with us if the container falls.

Mounting volume

For this you need to first create a directory in your system, for this demo purpose we are naming our directory as data-recover and we are giving 777 permissions using chmod command to our directory to avoid any permissions related issues.

mkdir /home/ubuntu/data-recover
cd /home/ubuntu/data-recover
chmod 777 .

Now we will run the elastic search container and we will mount this directory as a volume to the directory inside the container that is generating the data. We will mount this using -v flag in our docker run command

sudo docker run -it -p 9200:9200 -p 9300:9300 -d -v/home/ubuntu/data-recover:/usr/share/elasticsearch/data

Now if we will go to our directory and ls in our directory that we created as a volume so we will be seeing the data. Now if our container will be killed then we will be still having our data with us and we can again mount it to our docker container

Subscribe Now