Implemented Docker volume persistence solution for enhanced data management in containerized environments using the 'DockerDataGuard' project.

ยท

6 min read

Launch an ec2 instance and clone a project from git on that instance:

  1. Create a Directory:

    First, create a directory where you want to store your project and data. You can use the mkdir command to do this. For example:

     mkdir django-todo-vol
     cd django-todo-vol
    
  2. Clone the Project:

    If your project is hosted on a Git repository, you can use the git clone command to clone it into the my_project directory:

     git clone https://github.com/Pardeep32/django-todo-cicd.git
    

    Replace <repository_url> with the actual URL of your project's Git repository.

  3. Create a Directory for Docker Volumes:

    Now, create a directory to store Docker volumes. You can do this within the my_project directory:

     mkdir volumes
    

    This directory will be used to persist data from Docker containers.

  4. Install Docker:

    To install Docker on your instance, you can follow the official installation instructions for your specific Linux distribution. Here's a general example for installing Docker on Ubuntu:

    
     sudo apt update
     #install Docker
     sudo apt install -y docker.io
    

    Adjust these commands as needed for your specific distribution. After installing Docker, you can start the Docker service:

     sudo systemctl start docker
    

    You may also want to add your user to the docker group to run Docker commands without using sudo. Be cautious with this as it grants elevated privileges:

     sudo usermod -aG docker $USER
     reboot docker
     #You'll need to log out and back in or restart your instance for the group changes to take effect.
    

Here's an explanation of each line in the Dockerfile:

  1. FROM python:3: This line specifies the base image for your Docker image. In this case, you are starting with the official Python 3 image, which serves as the base for your application.

  2. WORKDIR /data: Sets the working directory within the container to /data. Any subsequent commands will be executed relative to this directory.

  3. RUN pip install django==3.2: This command installs Django version 3.2 using the pip package manager. It ensures that Django is available within the container for your application.

  4. COPY . .: This line copies the contents of the current directory (where your Dockerfile is located) into the /data directory of the container. This assumes that your Django project files are in the same directory as the Dockerfile.

  5. RUN pythonmanage.pymigrate: This command runs Django's migrate management command within the container. It is used to apply database migrations, ensuring that your database schema is up to date.

  6. EXPOSE 8000: This line informs Docker that the container will listen on port 8000. It doesn't actually publish the port; it's a way to document which ports the container expects to use.

  7. CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]: This specifies the default command to run when the container starts. It runs the Django development server (runserver) and binds it to all available network interfaces (0.0.0.0) on port 8000. This is a common way to run a Django application during development.

To ensure that port 8000 is accessible from external sources to your EC2 instance, you need to update the security group associated with the instance to allow incoming traffic on port 8000. Here are the steps to add port 8000 to your EC2 instance's security group:

  1. Identify Your Security Group:

    First, you need to identify the security group associated with your EC2 instance. You can do this by going to the AWS Management Console:

    • Navigate to the EC2 dashboard.

    • Click on "Instances" in the left sidebar to see a list of your instances.

    • Select the EC2 instance you're working with.

    • In the "Description" tab at the bottom, you'll find the security group name under "Security groups."

  2. Edit the Security Group Inbound Rules:

    Once you've identified the security group, follow these steps to add port 8000:

    • Go to the EC2 dashboard.

    • In the left sidebar, click on "Security Groups" under the "Network & Security" section.

  3. Select the Security Group:

    Find and select the security group associated with your EC2 instance from the list.

  4. Edit Inbound Rules:

    • In the "Inbound rules" tab, click the "Edit inbound rules" button.
  5. Add a New Rule:

    • Click the "Add rule" button.

    • In the "Port range" field, enter 8000.

    • In the "Source" field, you can specify the IP range or specific IP addresses that are allowed to access port 8000. To allow access from anywhere, you can use 0.0.0.0/0, but it's recommended to restrict access to specific IPs or IP ranges if possible for security reasons.

  6. Review and Save:

    • Review the rule you added.

    • Click the "Save rules" button to apply the changes to the security group.

Now, your EC2 instance's security group is configured to allow incoming traffic on port 8000. Make sure to keep your security group rules as restrictive as possible to enhance the security of your instance.

Run the docker image in order to make docker conatiner:

sudo docker run -d -p 8000:8000 django-app:latest

To check running conatiners:

docker ps

go to ec2 instance's public ip address. copy the address and open a new tab and in url paste that address 54.174.135.177:8000 it willl show running app.

In order to ensure that the task list data remains persistent even if the container is terminated or stopped, we need to implement volume storage.

i killed running docker container and again run the docker from same docker image by using following commands:

docker ps

docker kill <conatiner id>

docker rm <conatiner id>

docker run -d -p 8000:8000 django-app:latest

go back to new tab and refresh, the added list is disappear:

so the concept of volume is use to store the data of conatiner.

the following command is used to generate volume:

sudo docker volume create --name django-app-volume --opt type=none --opt device=/home/ubuntu/volumes/django-app-volume --opt o=bind

now, mount the volume to container and then store the data in that conatiner:

docker run -d --mount source=django-appvolume,target=/data -p 8000:8000 django-app:latest

After refreshing the page and adding items to the list, the responsibility of Docker volumes is to ensure data persistence even if the Docker container is terminated or stopped. The highlighted part is conatiner's data which is now stored in volume.

After entering the container, creating a 'scope.txt' file with content, terminating the container, and subsequently launching a new container using the 'docker run' command with the same volume ('django-app-volume') mounted, the 'scope.txt' file persists and remains accessible in the new container due to volume persistence.

After refreshing the page, the previously added content remains intact even after the Docker container has been terminated.

๐Ÿ™ Big Thanks for Diving into Docker Volumes! ๐Ÿ™

A heartfelt thank you to everyone who took the time to read my recent article on Docker volumes. Your interest in exploring this crucial aspect of containerization is truly appreciated.

I hope the article shed light on the importance of Docker volumes in managing data persistence and sharing between containers. By delving into the intricacies of volume management, I aimed to provide practical insights to enhance your Docker workflow and optimize your containerized applications.

Your engagement and support mean the world to me. Whether you're a seasoned Docker enthusiast or just starting your journey with containerization, your curiosity and dedication to learning inspire me to continue sharing knowledge in this ever-evolving field.

If you have any questions, feedback, or topics you'd like me to cover in future articles, please don't hesitate to reach out. Let's continue this journey of exploration and discovery together!

Thank you once again for your readership and enthusiasm. Here's to many more exciting discussions and insights on Docker volumes and beyond!

Warm regards,

Pardeep kaur

email id -

ย