Automating Django Deployment with Jenkins and Docker on AWS EC2 Instances

Automating Django Deployment with Jenkins and Docker on AWS EC2 Instances

Day 80 & 81: Jenkins CI/CD Pipeline to Deploy a Web Application

Efficient deployment is a cornerstone of successful software development. In this tutorial, we'll guide you through the process of setting up a Jenkins pipeline to automate the deployment of your Django web application using Docker. What makes this tutorial special is the utilization of two EC2 instances – a Jenkins master server and an agent server – to facilitate seamless deployment.

Section 1: Prerequisites and Project Setup

1.1 Prerequisites

Before initiating the deployment process, confirm that the following prerequisites are met:

  • Two EC2 instances for the Jenkins master and agent servers.

  • Jenkins installed and configured on the master instance.

  • JDK, Docker, and Docker Compose installed on both instances.

  • A DockerHub account for storing Docker images.

Section 2: Configuring Jenkins Master and Agent

2.1 Setting Up Jenkins Master

  1. Access your Jenkins master server and navigate to "Manage Jenkins" -> "Manage Nodes and Clouds."

  2. Click on "New Node" to add the agent EC2 instance.

  3. Name the node, select "Permanent Agent," and click "OK."

  4. Specify the necessary configurations, ensuring Docker is installed on the agent. Refer to Jenkins documentation for more details.

2.2 Creating a New Pipeline in Jenkins

  1. Access Jenkins and click on "New Item."

  2. Choose "Pipeline" and provide a name for your project.

  3. In the pipeline configuration, select "Pipeline script from SCM."

  4. Choose "Git" as the SCM, and enter your GitHub repository URL.

  5. Save the configuration.

Section 3: Building the Declarative CICD Pipeline

3.1 Modifying Jenkinsfile

Update your Jenkinsfile with the following steps to include agent configuration and deployment:

groovyCopy codepipeline {
    agent { label 'dev-server' }
    stages {
        stage('Code Clone') {
            steps {
                echo 'Cloning'
                git url: "", branch: "main"
        stage('Build') {
            steps {
                echo 'Building'
                sh 'docker build --no-cache -t my-note-app .'
        stage('Push to DockerHub') {
            steps {
                echo 'Pushing'
                withCredentials([usernamePassword(credentialsId:"dockerHub",passwordVariable:"dockerHubPass",usernameVariable:"dockerHubUser")]) {
                    sh "docker tag my-note-app ${env.dockerHubUser}/my-note-app:latest"
                    sh "docker login -u ${env.dockerHubUser} -p ${dockerHubPass}"
                    sh "docker push ${env.dockerHubUser}/my-note-app:latest"
        stage('Deploy') {
            steps {
                echo 'Deploying'
                sh "docker-compose down && docker-compose build --no-cache && docker-compose up -d --build"

Section 4: Configuring DockerHub Credentials

4.1 Jenkins Master

  1. Navigate to "Manage Jenkins" -> "Manage Credentials."

  2. Add your DockerHub credentials.

Section 5: Creating Docker Compose File

Include a comprehensive Docker Compose file in your GitHub repository. Configure services, networks, and volumes based on your Django app and any related services.

Section 6: Configuring GitHub Webhook

  1. In your GitHub repository, navigate to "Settings" -> "Webhooks" -> "Add webhook."

  2. Set the Payload URL to your Jenkins master server's webhook URL.

  3. Configure the webhook to trigger on push events.

Section 7: Testing the Pipeline

  1. Click on "Build Now" in Jenkins. It should deploy the app.

  2. Make a small change in your Django app code.

  3. Commit and push the changes to your GitHub repository.

  4. Observe Jenkins triggering the pipeline on the master server, with deployment occurring on the agent server.


You've successfully established a robust CICD pipeline for your Django web application, incorporating GitHub integration, Jenkins, Docker, and an agent-based approach. This comprehensive guide allows for easy customization to suit your specific development needs. Explore additional optimizations and enhancements to further enhance your deployment workflow.

Happy Learning!

Follow me on LinkedIn.