Understanding Docker Volumes: A Beginner's Guide with super easy Examples

Hitesh MishraHitesh Mishra
3 min read

What is a Docker Volume?

In Docker, a volume is a mechanism for persisting data generated by and used by Docker containers. Unlike the container's writable layer, which is removed when the container is deleted, volumes are designed to persist data beyond the container's life. Volumes are stored on the host file system outside of the container's filesystem, making them an ideal way to share data between multiple containers or persist data across container restarts and removals.

Why Do We Need Docker Volumes?

  1. Persistence: Volumes allow data to persist even after the container is removed.

  2. Sharing Data: Volumes can be used to share data between multiple containers.

  3. Decoupling Data from Containers: Volumes decouple the data from the container’s lifecycle, making the containers ephemeral and the data persistent.

  4. Performance: Volumes offer better performance compared to storing data within the container’s writable layer.

  5. Backup and Restore: Volumes can be easily backed up and restored, ensuring data safety.

How to Use Docker Volumes

Step-by-Step Guide with MongoDB Example

Let's walk through an example of using Docker volumes with a MongoDB container.

Step 1: Install Docker

Make sure Docker is installed on your system. You can download and install Docker from here.

Step 2: Create a Docker Volume

Create a volume using the docker volume create command.

docker volume create mongodata

Step 3: Run MongoDB with Docker Volume

Run a MongoDB container and attach the created volume.

docker run -d -p 27017:27017 --name mongodb -v mongodata:/data/db mongo
  • -d: Run the container in detached mode.

  • -p 27017:27017: Map port 27017 of the container to port 27017 of the host.

  • --name mongodb: Name the container "mongodb".

  • -v mongodata:/data/db: Mount the mongodata volume to /data/db in the container.

Step 4: Interact with MongoDB

You can now interact with your MongoDB instance. For example, use the MongoDB client to connect to the database:

docker exec -it mongodb mongo

This command opens an interactive shell to the MongoDB instance running inside the container.

Step 5: Verify Data Persistence

Even if the container is removed, the data will persist in the volume. Let’s test it.

Add Some Data

  1. Open the MongoDB shell:

     docker exec -it mongodb mongo
    
  2. Add data to a database:

     use mydatabase
     db.mycollection.insert({ name: "Docker Volume Example" })
    

Remove the Container

docker rm -f mongodb

Re-run MongoDB with the Same Volume

docker run -d -p 27017:27017 --name mongodb -v mongodata:/data/db mongo

Verify Data

  1. Open the MongoDB shell again:

     docker exec -it mongodb mongo
    
  2. Check the data:

     jsCopy codeuse mydatabase
     db.mycollection.find()
    

You should see the data you inserted earlier, proving that the data persisted across container removals.

Docker Volume Commands

Here are some essential Docker volume commands:

  • Create a volume:

      docker volume create <volume_name>
    
  • List volumes:

      docker volume ls
    
  • Inspect a volume:

      docker volume inspect <volume_name>
    
  • Remove a volume:

      docker volume rm <volume_name>
    
  • Remove all unused volumes:

      docker volume prune
    

Summary

Docker volumes are a powerful feature for persisting data beyond the lifecycle of containers. They are essential for data persistence, sharing data between containers, and decoupling data from containers. By using Docker volumes with MongoDB, we can ensure that our data is safe and persists even when the container is removed.

I hope this guide helps you get started with Docker volumes. Happy Dockering!

10
Subscribe to my newsletter

Read articles from Hitesh Mishra directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Hitesh Mishra
Hitesh Mishra

I am Hitesh Mishra, currently pursuing my bachelor of technology in computer science engineering from Graphic Era Hill University, Dehradun. I am always ready to have new experiences, meet new people and learn new things. 🔹Passionate about coding(problem solving) and web development. Currently learning web development. I have also created some projects like a full stack ecommerce website, full blog in which a user can create, edit, delete and update blog posts, forkfiy project in which users can find a recipe and instructions on how to make them! 🔹Currently learning ReactJS 🔹SKILLS: HTML, CSS, JAVASCRIPT, NODEJS, EXPRESSJS, MYSQL, MONGODB, C, C++. 🔹Check out my GitHub profile: https://github.com/hiteshmishra2103/ 🔹Linkedin: https://www.linkedin.com/in/hiteshmishra21/ Want to contact me?.📞 Just leave me a message on linkedin.