Automating Django Todo App Deployment on AWS EC2 with Jenkins, Docker, and Docker Compose

Guide to deploying Django Todo App from GitHub to AWS EC2 via Jenkins, automating Docker image creation, EC2 deployment, and pushing to Docker Hub.

Automating Django Todo App Deployment on AWS EC2 with Jenkins, Docker, and Docker Compose

Preparing the Environment

1. Launch an AWS EC2 Instance

  • Log in to your AWS Management Console.

  • Navigate to EC2 and launch an instance.

  • SSH into your EC2 instance.

2. Install Jenkins on EC2 Instance

3. Install Docker and Docker Compose on EC2 Instance

  •   sudo apt update
      sudo apt-get install docker.io docker-compose -y
      sudo usermod -aG docker $USER
      sudo reboot
    

    Note: After reboot, reconnect the instance.

4. Configure Jenkins

  • Check the status of Jenkins.

      sudo systemctl status jenkins
    
  • Open Jenkins on your EC2 instance (http://YOUR_EC2_PUBLIC_IP:8080).

    Run this command to find administrator password:

      sudo cat /var/lib/jenkins/secrets/initialAdminPassword
    
  • Install necessary plugins and set up the username and password for Jenkins dashboard.

Setting Up Jenkins Pipeline

1. Create Jenkins Pipeline Job

  • Create a new Pipeline job in Jenkins.

    New Item

  • Set the Git repository URL to https://github.com/robinthakur00/django-todo-app.git.

2. Define Jenkins Pipeline Stages

  • Create a Jenkinsfile at the root of the repository to define the deployment stages.

      pipeline {
          agent any
          stages {
    
              stage("Code Fetch From Git"){
                  steps{
                      git url: "https://github.com/robinthakur00/django-todo-app.git", branch: "master"
                  }
              }
              stage("Build Docker Image"){
                  steps{
                      sh "docker build -t django-todo-app ."
                  }
              }
              stage("Login And Push to DockerHub"){
                  steps{
                      withCredentials([usernamePassword(credentialsId:"dockerHub",passwordVariable:"dockerHubPass",usernameVariable:"dockerHubUser")]){
                      sh "docker login -u ${env.dockerHubUser} -p ${env.dockerHubPass}"
                      sh "docker tag django-todo-app:latest ${env.dockerHubUser}/django-todo-app:latest"
                      sh "docker push ${env.dockerHubUser}/django-todo-app:latest"
                      }
                  }
              }
              stage("Deploy the application"){
                  steps{
                      sh "docker-compose down && docker-compose up -d"
                  }
              }
          }
      }
    

3. Configure Jenkins Pipeline Job

  • Configure Jenkins pipeline job with the Jenkinsfile path (Jenkinsfile if it's at the root).

  • Define Docker Hub credentials in Jenkins Credentials Manager.

    Go to Manage Jenkins -> Credentials -> System -> Global credentials -> Add Credentials

Running the Jenkins Pipeline

1. Trigger Jenkins Pipeline Job

  • Run the Jenkins pipeline job, Click Build Now

  • Jenkins will clone the repository, build the Docker image, push it to Docker Hub, and deploy the Django Todo App onto the EC2 instance using Docker Compose.

  • Access the Django Todo App in (http://YOUR_EC2_PUBLIC_IP:8000)

    Note: This is a Django application that runs on port 8000. We need to open port 8000 in the security group.

Contributing:

Contributions are welcome! If you find any issues or want to improve this project, please open an issue or submit a pull request.

GitHub Link of Django App

Happy Learning ^_^

Did you find this article valuable?

Support Devops Related Articles by becoming a sponsor. Any amount is appreciated!