Docker Volumes🐋 - CKA

Docker Volumes🐋 - CKA

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:

     sudo docker build -t todo-app .
    

  1. Run the Container:
sudo docker run -dp 3000:3000 --name doc-todo todo-app

  1. Exec into the Container and Create a Folder:

     sudo docker exec -it <container-id> sh
     mkdir /app/test-demo
     exit
    

  1. Stop the container and remove it
sudo docker stop <container-id>
sudo docker rm <container-id>

  1. Again, run the container and exec into the container and u will see that test-demo directory is also removed
sudo docker run -dp 3000:3000 --name doc-todo todo-app

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

  1. In our system, all docker directory are present in /var/lib/docker
sudo cd /var/lib/docker
ls -lrt

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

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

    sudo docker exec -it <container-id> 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:

    sudo docker stop <container-id>
    sudo docker rm <container-id>
    

  1. Run the container again:

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

    sudo docker exec -it <container-id> 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.