CoDeKu DevOps Academy Blog - DevOps & Cloud Blogging Platform

What is Docker Volume 🐳

Story of the Docker Island 🏝️

Once upon a time, there was an island called Docker. The most people’s occupation on this island was cooking food. There was a kitchen for each chef on this magical island.

But there was a problem with each kitchen on this magical island. Every time a chef left their kitchen, everything such as recipes, groceries, and even the finished meals disappeared. 😲 In docker, we can consider a kitchen as a container. When we remove a container, all its data vanishes.😞

This problem is a huge headache for the chefs of this island. If the chef suddenly had to leave the kitchen, he would not be able to see any of the food he had cooked when he returned. 😭

So, they have to find a solution to this problem. Then one day, the island elders introduced a magical Cave called Volume. When considering this cave, we can point out some special points.📝

  • The Cave (Volume) was located outside all the kitchens.⛰️
  • Each chef could connect their kitchen to the cave and store recipes, ingredients, or finished meals there. 🍽️
  • Even if a chef left the kitchen, the items in the cave stayed safe. 🤩

With this cave, chefs could solve their problem and satisfy their customers.

Bring the story to the Programming World🔄

Let’s make MongoDB Container without using a Docker Volume ⛰️

This runs MongoDB normally. But data is stored inside the container. We can put some data using the mongo shell. For that,

docker exec -it mongo-without-volume mongosh We can navigate into the Mongo db shell using this command. Now we can add data like below.

After adding data, we can check whether the data is added successfully using the db.users.find() command. So if we remove our container, all the entered data vanishes. Let’s check this.

Here, we can see there is no data in the mongo-without-volume container. Since this is a very troublesome task, let’s focus on how to maintain data without losing it using docker volumes.

Let’s make a MongoDB Container using a Docker Volume ⛰️

Now we have a docker container for our mongo db database with a volume. Here we can see there are two records. Let’s check whether this data vanishes or not when we remove our container.

After removing and recreating that specific container, we can see that two records are still here. So, we can get an idea of what is the purpose and how works Docker Volumes.

Also, this Docker volume can be considered a commonly used location on our computer and Docker container. This means, let’s say there is a file in the docker container, but we cannot access it on our computer. But we can access it using the common location place called volume. Let’s see how to do that with our story.

Nginx food without using a Docker Volume 🍕

We can create a container named nginx-without-volume like this and run it on our machine on port 9090. So that, we can open our web browser and navigate to localhost:9090 Then we can see the default nginx welcome page.

But here, can we update this page? can we change the default nginx index.html page? No right? That is because, it’s inside the container, we cannot access it from our local PC. So that we have to use the cave called volume. For that, first, you have to choose a specific place on your machine and then you can create a file called index.html.

Nginx food using a Docker Volume 🍕

We can connect our machine’s specific place with the container using this command. Let’s dive into the deep of this command.

-v E:\Projects\Docker\nginx-with-volume This line can be considered as the directory on our Windows machine that we are mounting into the container. /usr/share/nginx/html This is the path inside the container where the host directory will be mounted. The :ro at the end stands for read-only, meaning the container can read the files from your host, but can’t modify them. The :ro at the end stands for read-only, meaning the container can read the files from your host, but can’t modify them.

We can host our index.html file using Nginx like this. Without volume, we cannot customize the pages as we wish. But we can do that using the Docker Volume.

Share This Article

Ramitha Heshan
Ramitha Heshan

I am a dedicated Software Engineering student at Cardiff Metropolitan University. I am familiar with various programming languages including JavaScript, PHP, Java, and Python. Further, I completed a couple of academic projects and personal projects using multiple languages and technologies. I am always willing to learn different technologies.

Articles: 5

Leave a Reply

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