Diving Deeper into Docker: Understanding Volumes

Diving Deeper into Docker: Understanding Volumes

Did you know? Docker containers don’t store persistent data by default!

This means that all the data inside is lost when you stop or remove a container. There's no built-in backup to save your data in cases like power failures or accidental container removal.

So, how can we ensure that our data survives even when a container stops?

The answer is Docker Volumes! 🌟

Let’s look at how it works!

What is Volume

In Docker, a Volume is a storage mechanism that allows data to persist beyond the lifecycle of a container. Containers are typically ephemeral, meaning that their data is lost when they are stopped or removed. Volumes solve this issue by providing a way to store data persistently and independently of the container's lifecycle.

Why use Volumes

  • Data remains even if the container is stopped or deleted.

  • Volumes are managed by Docker, separate from the host file system.

  • You can easily share data between containers using volumes.

  • Volumes are optimized for Docker and often provide better I/O performance than host file system bindings.

How it Works

To understand the importance of volumes in Docker, let’s look at how it works without a volume and with a volume:

Running a MongoDB Container Without a Volume

Let’s run a MongoDB container without a volume to understand what happens to the data.

1️⃣ Pull the MongoDB Image
First, pull the Mongo image:

docker pull mongo

2️⃣ Run the MongoDB Container
Use this command to run the container:

docker run -d --name mongo-without-vol mongo

3️⃣ Connect to the MongoDB Container
Access the MongoDB shell inside the running container:

docker exec -it mongo-without-vol mongosh

4️⃣ Store Data in MongoDB
Insert some data into the MongoDB database (e.g., a collection of documents):

db.testCollection.insert({ name: "Test Data", status: "Temporary" })

At this point, the data is stored in the MongoDB container.

5️⃣ Stop and Remove the Container
Let’s stop and delete the container to see what happens to the data:

docker stop mongo-without-vol
docker rm mongo-without-vol

What’s the Problem?

When the container is removed, the data stored inside it is also erased, as it was only temporarily stored in the container’s writable layer.

This highlights the importance of using volumes for persistent storage in Docker. Volumes ensure that your data is preserved even if the container is stopped or deleted.

Running a MongoDB Container With Volume

Now let’s run a MongoDB container with Volume

1️⃣ Create a Volume

docker volume create volume_name

To check if the volume was created or not, we run the following command,

docker volume ls

2️⃣ Run the MongoDB Container

Use this command to run the container with the created volume

docker run -d --name mongo-with-vol -v volume_name:/data/db mongo

Here, even if you don’t follow the first step and directly give volume_name, it will also create a volume of that name.

3️⃣ Connect to the MongoDB Container

docker exec -it mongo-with-vol mongosh

4️⃣ Store Data in MongoDB

db.testCollection.insert({ name: "Test Data", status: "Temporary" })

5️⃣ Stop and Remove the Container

docker stop mongo-with-vol
docker rm mongo-with-vol

Here’s how it works in action:

Using a Previously Created Volume

Now that we’ve created a volume and stored data in it, let’s see how we can reuse that volume in another container to verify data persistence.

1️⃣ Run a New MongoDB Container with the Same Volume

Start a new MongoDB container and attach the previously created volume:

docker run -d --name mongo-with-vol-2 -v volume_name:/data/db mongo

Replace volume_name with the name of the volume you created earlier.

2️⃣ Connect to the New MongoDB Container

docker exec -it mongo-with-vol-2 mongosh

3️⃣ Verify Data Persistence

Check if the data you stored earlier is still available in the new container:

db.testCollection.find()

You should see the data previously inserted, confirming that the volume retains the data.

Here’s how it works in action:

Outro

Volumes are an essential part of docker to keep data persistent. They allow you to persist data, share it, and ensure it is safe and reliable. In this blog, we saw how volumes help retain data even if the container is deleted and reuse those volumes in a new container.

I hope you found this blog helpful and that it will assist you in your DevOps journey.

Thank you for reading, and happy coding! 👨‍💻🚀