Day 27 Task: Jenkins Declarative Pipeline with Docker

Day 27 Task: Jenkins Declarative Pipeline with Docker

Streamlining CI/CD with Docker-Integrated Jenkins Declarative Pipelines

ยท

3 min read

Day 26 was all about a Declarative pipeline, now its time to level up things, let's integrate Docker and your Jenkins declarative pipeline

๐Ÿ”ถ Use your Docker Build and Run Knowledge

docker build - you can use sh 'docker build . -t <tag>' in your pipeline stage block to run the docker build command. (Make sure you have docker installed with correct permissions.

docker run: you can use sh 'docker run -d <image>' in your pipeline stage block to build the container.

How will the stages look

stages {
        stage('Build') {
            steps {
                sh 'docker build -t trainwithshubham/django-app:latest'
            }
        }
    }

๐Ÿ”ถ Task-01: Create a docker-integrated Jenkins declarative pipeline.

  • Creating a Docker-Integrated Jenkins Declarative Pipeline
    1. Set Up Jenkins:

    • Install and configure Jenkins on your server or cloud platform.

2. Install Docker:

  • Ensure that Docker is installed on the same machine as Jenkins.

3. Create a New Item:

  • Go to your Jenkins dashboard and click on "New Item."

  • Enter a name for your project, select "Pipeline," and click "OK."

4. Configure Pipeline:

  • In the project configuration, scroll down to the "Pipeline" section.

  • Select "Pipeline script" from the "Definition" dropdown.

  • Use the above-given syntax using sh inside the stage block

      pipeline {
          agent any
          stages{
              stage ('Code Clone') {
                  steps {
                      git url : 'https://github.com/Chandreshpatle28/django-todo-cicd-Jenkins.git' , branch : 'develop'
                  }
              }
              stage ('Build') {
                  steps {
                      sh 'docker build . -t  django-todo-cicd-jenkins:latest'
                  }
              }
              stage ('Testing') {
                  steps {
                      echo 'testing'
                  }
              }
              stage ('Deploy') {
                  steps {
                      sh 'docker run -d -p 8000:8000 django-todo-cicd-jenkins:latest'
                  }
              }
          }
      }
    

  • Now Save and build to start the pipeline.

  • Console output.

  • You will face errors in case of running a job twice, as the docker container will be already created, so for that do task 2

๐Ÿ”ถ Task-02: Create a docker-integrated Jenkins declarative pipeline using the docker groovy syntax inside the stage block.

  • Now change something from GitHub. and I also added a docker-compose.yml file here.

  • Now save and Build Now start the pipeline.

  • You won't face errors, you can Follow this documentation.

In conclusion, creating a Docker-integrated Jenkins Declarative Pipeline empowers you to seamlessly incorporate Docker into your CI/CD workflow. By combining the power of Jenkins and Docker, you can automate the building and running of Docker containers as part of your software deployment process. This approach streamlines the development lifecycle, allowing you to efficiently manage containerized applications with consistency and reliability. Docker-integrated pipelines offer a flexible and effective way to harness the benefits of containerization while leveraging the capabilities of Jenkins for automated and controlled deployments.


Happy Learning:)

Stay in the loop with my latest insights and articles on cloud โ˜๏ธ and DevOps โ™พ๏ธ by following me on Hashnode, LinkedIn (https://www.linkedin.com/in/chandreshpatle28/), and GitHub (https://github.com/Chandreshpatle28).

Thank you for reading! Your support means the world to me. Let's keep learning, growing, and making a positive impact in the tech world together.

#Git #Linux Devops #Devopscommunity #90daysofdevopschallenge #python #docker #Jenkins

Did you find this article valuable?

Support Chandresh Patle's Blog by becoming a sponsor. Any amount is appreciated!

ย