Objective:-
In this Blog, we will implement Volumes in Docker Containers and we will see how we can Protect our Data(Files, Folders etc..) in our Containers.
What are Volumes in Docker?
Volumes are a method to Protect our data in Containers from getting lost. Suppose we have any file xyz in a container x and if this container is killed or removed then the file or data inside the container will also get removed. So we need a solution and here come Volumes. If we implement Volumes in containers then the data is safe if containers get killed or removed.
Creating a Server with Docker Installed:-
I have already a blog on Setting up a Server on AWS.
This is the Blog Link:-
In this blog, I have 2 Instances launched with t2.medium but here follow those steps launch one instance with the t2.micro instance type and the rest will be the same. Connect it using SSH in your system.
Now we have connected our Server using SSH with our System and we will install Docker On our Server. So run these Commands:
sudo apt update sudo apt install docker.io -y sudo systemctl start docker sudo systemctl enable docker sudo usermod -aG docker ubuntu
Verify the Installation by the command:
docker --version
Now create a directory named docker-project and navigate to the directory. so run the commands:
mkdir docker-projects cd docker-projects
Create another directory in this folder named django-app and navigate to it. so run the commands:
mkdir django-app cd django-app
Now here in the django-app folder run the command:
git clone https://github.com/LondheShubham153/django-todo-cicd.git
Navigate to the cloned Repository by the command:
cd django-todo-cicd
We have already the Dockerfile so we will directly run the Command:
sudo docker build . -t django-app
Now run the command:
sudo docker run -d -p 8000:8000 django-app:latest
- Open the PORT 8080 on your EC2 Instance and Copy your Instance Public IPv4 address.
After Copying the Public IPv4 address of your instance paste it in the browser with 8000 at the end. This is mine:
We will see that our Project is Deployed and Running on the Server.
Creating Volume for our Containers:-
Run this command 2 times:
cd . .
Now create a directory named volumes to it and navigate in this directory by the Commands:
mkdir volumes cd volumes
Create another folder in the volumes directory named django-app-volume and navigate also. So run the Commands:
mkdir django-app-volumes cd django-app-volumes
Now in the django-app-volumes directory run the Command:
pwd
Keep the path saved anywhere or remember it.
Okay so here run the Command:
sudo docker volume create --name django-app-volume --opt type=none --opt device=/home/ubuntu/docker-projects/volumes/django-app-volume --opt o=binddjango-app-volume
To verify that Volume is there run the Command:
sudo docker volume ls
We have to delete the old Container as it's not mounted with volume so first of all run the Command:
sudo docker ps
Copy the Container ID and kill the Container by the Command:
sudo docker kill your-container-id
The Container is killed now remove it by the Command:
sudo docker rm your-container-id
We don't have any Containers now.
Now create a Container with Volume by the Command:
sudo docker run -d --mount source=django-app-volume,target=/data -p 8000:8000 django-app:latest
The Container is created with Volume to verify it runs the Command:
sudo docker ps
Copy your Container ID.
Now go inside the Container by the Command:
sudo docker exec -it your-container-id bash
After coming inside the Container create a file, write a line inside the file and then exit. So run these Command:
touch secret-file.txt echo "My secret" > secret-file.txt exit
Now kill and remove this Container. I have already told how to kill and remove the Container.
Here we will see the magic. Now create a Container again by the Command:
sudo docker run -d --mount source=django-app-volume,target=/data -p 8000:8000 django-app:latest
Again Copy your Container ID and go Inside your Container by the Commands:
sudo docker ps // To Copy Container ID sudo docker exec -it your-container-id
We are inside the Container run the Command:
ls
Here we can see that our file which was in the old container is here also. So anything happens in the Container our data is safe. If production gets destroyed also our data will be safe.
Thanks.