DevOps(Day-35) : Jenkins Declarative Pipeline with Docker

 

  • Task-1 : Using 'sh' syntax

  • Task-2 : Using 'docker' groovy syntax

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:-

COPY

stages {
        stage('Build') {
            steps {
                sh 'docker build -t biswaraj/app:latest'
            }
        }
    }
  1. Navigate to Jenkins and click on new item.

  2. Create a pipeline project.

  3. Configure the pipeline by writing the groovy script for the node app.

    COPY

      pipeline {
          agent any
    
          stages {
              stage('Code Fetch') {
                  steps {
                      git 'https://github.com/Biswaraj/nodejs.git/'
                  }
              }
              stage('Build'){
                  steps {
                      sh 'sudo docker build . -t Biswaraj/nodejs-app:latest'
                  }
              }
              stage('Run'){
                  steps {
                      sh 'sudo docker run -d -p 8000:8000 biswaraj/nodejs-app:latest'
                  }
              }
          }
      }
    

  4. Save and click on Build now.

  5. Check the output of the build.

  6. Now, you can use the URL to check if the application is live.

  1. Follow all the same steps to create a project as above.

  2. In the configuration write the groovy script to integrate docker with Jenkins.

    COPY

      pipeline {
          agent any
          stages {
              stage('Build') {
                  agent {
                      docker { 
                          image 'biswaraj/nodejs-app:latest'
                          reuseNode true
                      }
                  }
                  stage{
                      echo "built"
                      sh 'nodejs --version'
                  }
    
                  }
               stage('Run') {
                  steps {
                      sh 'sudo docker run -d --name nodejsapp biswaraj/nodejs-app:latest'
                  }
              }
              }
          }
    

Thanks for reading my article. Have a nice day.

Comments

Popular posts from this blog

DevOps(Day-97): Meta-Arguments in Terraform

DevOps(Day-95): Auto Scaling with Terraform

DevOps (Day-1) Introduction