Docker Volume Explained : Day 28 of 40daysofkubernetes

Shivam GautamShivam Gautam
3 min read

Introduction

In the world of containerization, Docker has revolutionized how we build, ship, and run applications. However, one common challenge developers face is managing data persistence within containers. By default, data inside a Docker container is ephemeral, meaning it is lost when the container stops or is deleted. To address this issue, Docker provides a solution known as volumes. Docker volumes allow us to store data independently of the container's lifecycle, ensuring that our important data is preserved even if the container is removed.

Let's explore Docker volumes with examples.

Docker Volumes: An Overview

Docker volumes are a method for persisting data generated and used by Docker containers. By default, data inside a container is ephemeral, meaning it disappears when the container stops or is removed. Volumes allow you to store this data outside the container's lifecycle, enabling data persistence even if the container is deleted or recreated.

Why Use Docker Volumes?

  • Data Persistence: Volumes enable you to store and reuse data across multiple containers and container runs.

  • Sharing Data: You can share data between multiple containers.

  • Decoupling Data: Volumes decouple the storage of data from the container itself, which is helpful for backups, migrations, or upgrades.

Types of Docker Volumes

  1. Anonymous Volumes: Automatically created by Docker without a name. Usually created when a VOLUME instruction is specified in a Dockerfile without a name.

  2. Named Volumes: Created with a specific name, allowing for better management and reuse.

  3. Host Volumes (Bind Mounts): Mounts a directory from the Docker host into the container.

Example: Named Volume

Let's go through an example to see Docker volumes in action.

  1. Clone the GitHub Repository for Demo:
git clone https://github.com/docker/getting-started-app.git
cd getting-started-app
  1. Create a Dockerfile:

    Create a Dockerfile in the getting-started-app directory with the following content:

FROM node:18-alpine
WORKDIR /app
COPY . .
RUN yarn install --production
CMD ["node", "src/index.js"]
EXPOSE 3000
  1. Build the Docker Image:

     docker build -t dockerstodo .
    

  1. Run the Container:

  1. Exec into the Container and Create a Folder:

     docker exec -it todo3 sh
     mkdir /app/test-demo
     exit
    

  1. Stop the container and remove it

  1. Again run the container and exec into the container and u will see that test-demo directory is also removed

  1. To make our data persistent, we create and use a named volume:

  1. In our system , all docker directory are present in /var/lib/docker

Our data_vol is present in this directory , u can see in above image.

  1. Run the Container Again:
docker run -v data_vol:/app -dp 3000:3000 --name=todo3 dockerstodo
  1. Exec into the Container and Create a Directory:

    docker exec -it todo3 sh
    mkdir /app/test-demo
    exit
    

  1. When we check our system all data come in our system including test-demo

  1. Check the Volume Persistence:

    Stop and remove the container:

    docker stop todo3
    docker rm todo3
    

  1. Run the container again:

    Exec into the container and verify that the test-demo directory is still present:

    docker exec -it todo3 sh
    ls /app
    

You will notice that the test-demo directory remains intact, demonstrating how Docker volumes allow data to persist even after a container is stopped and removed.

Conclusion

Docker volumes are a powerful feature for managing persistent data in your Dockerized applications. They are easy to use, offer flexibility, and ensure that your data is safe even when containers are removed or recreated.

Resources I used

0
Subscribe to my newsletter

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

Written by

Shivam Gautam
Shivam Gautam

DevOps & AWS Learner | Sharing my insights and progress ๐Ÿ“š๐Ÿ’ก|| 1X AWS Certified || AWS CLoud Club Captain