Docker Volumes: Everything You Need to Know

Docker Volumes: Everything You Need to Know

·

5 min read

Docker has revolutionized the way we develop, ship, and deploy applications by containerizing them. However, containerization comes with its own set of challenges, and one of the most crucial aspects to understand is Docker volumes. Docker volumes enable data persistence and flexibility in handling data within containers. In this blog, we'll explore what Docker volumes are, why they are needed, the different types available, and essential commands, and provide a short demo to illustrate their importance.

Requirements

  1. Docker CLI installed

What Are Docker Volumes?

Docker volumes are a mechanism for persistently storing and managing data used by Docker containers. While containers are designed to be ephemeral meaning any data created or modified inside a container will be lost once the container is removed., volumes provide a way to decouple the data from the container itself, ensuring data durability and separation of concerns. Volumes can be used to share data between containers, back up data, and even manage application configurations.

Why Are Volumes Needed?

Docker containers are ephemeral by nature, This characteristic is great for stateless applications (those apps that do not store any state), but when dealing with databases, configuration files, or any form of persistent data, Docker volumes become essential.

Here are some key reasons why Docker volumes are needed:

Types of Docker Volumes

  1. Named Volumes: These are explicitly (manually by us) created and named volumes that are independent of the container's lifecycle. They are ideal for long-term data storage and sharing between containers.

     docker volume create <volume_name>
    
     docker run -d --name <container_name> -v <volume_name>:<container_path> <image_name>
    
  2. Host Bind Mounts: Host bind mounts allow you to mount a specific file or directory from the host machine (your PC) into the container. Changes in the container reflect on the host, and vice versa.

     docker run -d --name <container_name> -v <host_path>:<container_path> <image_name>
    
  3. Anonymous Volumes: These volumes are created implicitly when a container is started and doesn't specify a volume name or bind mount. They are typically short-lived and tied to the container's lifecycle.

     docker run -d --name <container_name> -v <container_path> <image_name>
    

--mount vs --v

While both flags are used to create volume, the first one is more verbose than the latter. --v is a straightforward way to specify volume options when running a Docker container. For advanced configuration, you can use --mount they offer greater flexibility.

Essential Docker Volume Commands

  • Creating a Named Volume: (This will create a named volume which is made inside the docker volume folder )

      `docker volume create mydata`
    
  • Creating a Container with a Named Volume:

      docker run -d --name myapp -v mydata:/app/data myimage
    
  • List Volumes:

      docker volume ls
    
  • Inspect a Volume:

      docker volume inspect mydata
    
  • Removing a Volume:

      docker volume rm mydata
    

Demo - Creating an Nginx container and using docker volume with it.

In this demo, we'll create a Nginx container and use a Docker volume to persistently store a custom HTML file. For those who do not know about Nginx, it's a web server used to serve HTML files. This will demonstrate how to set up a Docker volume and use it with a container.

Step 1: Create a Docker Volume

First, let's create a Docker volume named html_volume that we'll use to store our custom HTML file.

docker volume create html_volume

Step 2: Create a Custom HTML File

Next, create a custom HTML file named index.html with some content. You can use any text editor to create this file.

<!DOCTYPE html>
<html>
<head>
    <title>My Custom Web Page</title>
</head>
<body>
    <h1>Welcome to My Custom Web Page!</h1>
    <p>This content is served by Nginx from a Docker volume.</p>
</body>
</html>

Save this index.html file to a directory on your local machine. We will reference this file to the container using host bind mount (the other volume type).

Step 3: Run the Nginx Container with the Volume

Now, let's run an Nginx container, mount the html_volume volume created in step 1, and use our custom HTML file as the web content.

docker run -d --name mynginx \
  -p 80:80 \  # port on which contianer is accessible
  -v html_volume:/usr/share/nginx/html \  # mounting the named volume so that our data (contianer html file) will get stored here.
  -v /path/to/your/index.html:/usr/share/nginx/html/index.html:ro \   # override the default html file of nginx using host bind mount.
  nginx # actual image name

Explanation of the command:

  • -d: Runs the container in detached mode so that you can still use your terminal after starting the container.

  • --name mynginx: Names the container as "mynginx" for easy reference.

  • -p 80:80: Maps port 80 from the host to port 80 in the container for web access.

  • -v html_volume:/usr/share/nginx/html: Mounts the html_volume volume to the Nginx container's default web content directory.

  • -v /path/to/your/index.html:/usr/share/nginx/html/index.html:ro: Mounts your custom index.html file into the container's web directory, and the :ro flag makes it read-only inside the container.

  • nginx: Specifies the Nginx image to use.

Step 4: Access Your Custom Web Page

With the Nginx container running, open a web browser and navigate to http://localhost. You should see your custom HTML page served by Nginx.

Step 5: Cleanup

To stop and remove the Nginx container, you can run the following commands:

docker stop mynginx
docker rm mynginx

Remember that the html_volume Docker volume still exists and can be reused for other containers or applications. You need to delete it manually.

Conclusion

We've successfully explored Docker volumes and learned how to leverage them for data persistence in containers. Docker volumes are a vital tool for managing data, and they play a significant role in building robust containerized applications.