Day 19 Task: Docker volumes
What is docker volumes ?
Docker volumes is nothing but if the container is killed the data of the container will also be killed to overcome this we will use docker volumes.
Docker volumes are used to persist data generated and used by Docker containers, allowing that data to survive container restarts, updates, or even deletion of the container. Here are the main reasons why volumes are crucial in Docker:
1. Data Persistence
By default, data stored inside a Docker container is lost when the container stops or is removed. Volumes allow you to store data outside of the container’s filesystem, ensuring that it persists across container restarts or recreations.
For example, if you are running a database inside a Docker container, the database files need to persist, so when the container is restarted, the data is still available.
2. Decoupling Data from Containers
- Volumes provide a way to separate the data from the container’s lifecycle. You can back up, migrate, or share data independently of the containers that use it.
3. Ease of Sharing Data Between Containers
- Volumes can be shared and reused among multiple containers, making it easy to share data and configurations between containers. For instance, multiple containers can read from or write to the same volume, facilitating communication and data exchange.
4. Improved Performance
- Volumes are managed by Docker and are optimized for performance. They are generally faster than storing data inside the container’s writable layer, especially on Linux, because volumes bypass the file system layer of the container.
5. Better Management of Sensitive Data
- Volumes can be used to store sensitive data such as configuration files, secrets, or certificates. They offer more secure storage compared to embedding this data inside the image or passing it through environment variables.
6. Simplified Backup and Recovery
- Data stored in volumes can be easily backed up and restored, making it easier to manage application data lifecycle.
7. Separation of Concerns
- Volumes allow developers to separate application logic from data management. Developers can focus on the application code, while data management, such as backups or migrations, can be handled separately.
8. Flexibility with File Storage Locations
- You can use volumes to mount files or directories from the host machine into the container. This allows you to use existing files or directories as part of your containerized application without copying them into the container image.
Now we will create the docker volumes
Step 1:Here we have taken the code from github ,basically developer is gone give the code to us so we have taken the code from github.
Step 2: Here we as a devops engineer has to create the docker file.
Step 3: After creating the docker file we have to build the file as shown in the below image.
Step 4 : Now the docker image will be created
Step 5 : Now we should run the container
Step 6 : To go inside the docker container the command is docker exec -it [container_id] bash as shown below the image. Here we have created a file called test.log inside the container.
Step 7 : lets say the container has been killed or destroyed so file created inside the container (test.log) will also be deleted.
Step 8: After running the container again the file (test.log) will not be be in the container as shown in the below image.
Step 9 : To get the test.log file again we are using the concept if docker volumes we should give the command as shown in the below image.
Step 10 : To check whether the volume has been created or not we use docker volume inspect
Step 11 : Now here we can see the test.log file is inside the container and also in the file system .
THANK YOU FOR READING!
Subscribe to my newsletter
Read articles from Manasa J directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by