Day 27 Task: Jenkins Declarative Pipeline with Docker

Day 27 Task: Jenkins Declarative Pipeline with Docker

ยท

3 min read

This is #90DaysofDevops challenge under the guidance of Shubham Londhe.

Day 27 TASK

How to Create a Jenkins Declarative Pipeline

Using Docker in Your Pipeline

  • Docker is like a special tool for developers. It helps us create and run applications in the same way, no matter where we want to use them. Now, let's find out how we can bring Docker into our Jenkins pipeline.

Understanding Docker Commands

We have two important commands to remember:

  1. docker build: This command makes our Docker image. In our pipeline, we can use sh 'docker build . -t <tag>' to make this happen.

  2. docker run: Once we have our image ready, we want to start using it. In our pipeline, we can use sh 'docker run -d <image>' to begin the process.

  • Here's an idea of how our pipeline stages might look:
stages {
    stage('Build') {
        steps {
            sh 'docker build -t trainwithshubham/django-app:latest'
        }
    }
}

  • To begin, set up an EC2 instance and install Jenkins on it. After installation, access Jenkins using the public IP of the EC2 instance and port 8080 in your web browser.

  • Once Jenkins is accessible, navigate to the Jenkins Dashboard and click on "New Item" to proceed with creating a new project.

  • In this step, you'll need to provide a name for your project and choose the project type as "pipeline." This selection is important because we'll be creating a Jenkinsfile to define our pipeline further.

  • After selecting the project type and providing the project name, click on the "OK" button.

  • This action will lead you to the "Project Configuration" section where you can further configure your pipeline settings.

  • In the "Project Configuration" section, navigate to the "Pipeline" section. From there, select "Pipeline script" as the definition type for your pipeline.

  • Next, it's time to write a basic Pipeline Script for your react-django application.

pipeline {
    agent any 

    stages {
        stage('Code') { 
            steps {
                git url: 'https://github.com/Akashdhengale/django-todo-cicd.git' , branch: 'develop' 
            }
        }

        stage('Build') { 
            steps {
                sh 'docker build . -t django_app_img:latest' 
            }
        }

        stage('Test') { 
            steps {
                echo "Testing" 
            }
        }

        stage('Deploy') { 
            steps {
                sh "docker run -d --name django_react_app_jenkins -p 8000:8000 django_app_img:latest"
            }
        }
    }
}
  • Once you've finished writing the Pipeline Script, click on the "Save" button to save your pipeline configuration.

  • Then, to initiate the build process, manually click on the "Build Now" tab.

  • After the build process is complete, you can review the output by clicking on the "Console Output" tab. This tab will provide detailed information about the execution and outcome of your pipeline build.

  • After confirming that the pipeline has run successfully, you can review the multi-stage view by clicking on the "Full Stage View" option available on the main page of your project. This view provides a comprehensive overview of all stages executed in your pipeline.

  • If you attempt to run the pipeline job again, you may encounter errors due to the Docker container already being created.

  • To address this issue, proceed to Task 2 for further instructions on resolving this challenge. To rerun the pipeline, simply click on the "Build Now" button once more.

  • It will throw the error.

  • To resolve the error and ensure smooth execution of the pipeline, click on "Save" after making any necessary adjustments. Then, proceed to click on "Build Now." This step should work fine now because we have dockerized the application and attempted to access it.

I hope you found value in this blog post. If you did, don't forget to follow and give it a clap ๐Ÿ‘ to show your support! Consider subscribing to my hashnode newsletter to stay updated on future posts.

Thank you for taking the time to read! ๐Ÿ’š

Did you find this article valuable?

Support Akash Dhengale by becoming a sponsor. Any amount is appreciated!

ย