Docker Volume Explained : Day 28 of 40daysofkubernetes
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
Anonymous Volumes: Automatically created by Docker without a name. Usually created when a
VOLUME
instruction is specified in a Dockerfile without a name.Named Volumes: Created with a specific name, allowing for better management and reuse.
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.
- Clone the GitHub Repository for Demo:
git clone https://github.com/docker/getting-started-app.git
cd getting-started-app
Create a Dockerfile:
Create a
Dockerfile
in thegetting-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
Build the Docker Image:
docker build -t dockerstodo .
- Run the Container:
Exec into the Container and Create a Folder:
docker exec -it todo3 sh mkdir /app/test-demo exit
- Stop the container and remove it
- Again run the container and exec into the container and u will see that
test-demo
directory is also removed
- To make our data persistent, we create and use a named volume:
- 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.
- Run the Container Again:
docker run -v data_vol:/app -dp 3000:3000 --name=todo3 dockerstodo
Exec into the Container and Create a Directory:
docker exec -it todo3 sh mkdir /app/test-demo exit
- When we check our system all data come in our system including test-demo
Check the Volume Persistence:
Stop and remove the container:
docker stop todo3 docker rm todo3
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
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