DevOps, Day - 27

DevOps, Day - 27

Jenkins Declarative Pipeline with Docker

ยท

3 min read

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

Let's use our 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.



Let's Begin ๐Ÿ”ฅ


Step 1: Click on localhost:8080

Step 2: Create a New Jenkins Pipeline

  • Click on "New Item" on the Jenkins homepage.

  • Enter a name for your pipeline (e.g., "My-new-pipeline") --> Choose "Pipeline" as the project type --> Click "OK."

Step 3: Configure the Pipeline

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

  • Select the "Pipeline script" option.

  • Write the pipeline script as shown below.

  •   pipeline {
          agent any
          stages{
              stage('Code'){
                  steps{
                      git url:'https://github.com/vbvsk/django-notes-app.git', branch:'main'
                  }
              }
              stage('Build'){
                  steps {
                      sh 'docker build -t vk-notes-app .'
                  }
              }
              stage('Deploy'){
                  steps{
                      sh 'docker run -d vk-notes-app'
                  }
              }
          }
      }
    

Step 4: After saving, click --> Build Now.

Step 6: Click on #3 to view the console output.


Let's Continue with further tasks๐Ÿš€๐Ÿ”ฅ


To incorporate Docker within the stage block of your Jenkins declarative pipeline, you will need to ensure that both the Docker Pipeline and Docker plugins are installed and configured.

Additionally, you should aim to publish your Docker image to a repository on Docker Hub or another suitable container registry.

Step 1: You can install these plugins by navigating to Dashboard --> Manage Jenkins --> select Manage Plugins under system configuration --> Download Now.


Step 2: Now we can see our docker image is created...

Step 3: Now to make changes in your pipeline code, you need your image which is available in the docker hub, so push the image to the docker hub as shown...

Step 4: we can see our image is pushed in the docker hub...


Step 5: Now change the pipeline code as shown...

pipeline {
    agent any
    stages {
        stage('Build') {
            steps {
                echo 'container build'
            }
            agent {
                docker {
                    image 'vrishnishreevb/vk-notes-app:latest'
                    reuseNode true
                }
            }
        }
        stage('Deploy') {
            steps {
                sh 'docker run -d -p 8081:80 vrishnishreevb/vk-notes-app:latest'
            }
        }
    }
}

Step 6: Click on Save --> Build Now.

Step 7: we can also see that our container is running in the 8081 port :)


Dragon Ball Goku Sleeping GIF | GIFDB.com


Thank you so much for reading.

Follow me on LinkedIn to see interesting posts like this : )

Linkedin

ย