Skip to main content

Command Palette

Search for a command to run...

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

Updated
2 min read
DevOps(Day-27) : Jenkins Declarative Pipeline with Docker
B

I like to explore the technology in DevOps area where I write blog about my learning each day on the tools that is mostly used in Industries for DevOps practices. You can go through my blogs and reach me out in LinkedIn for any suggestions.

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 bandank/app:latest'
            }
        }
    }

Task-1 : Using 'sh' syntax

  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.

     pipeline {
         agent any
    
         stages {
             stage('Code Fetch') {
                 steps {
                     git 'https://github.com/Bandank/nodejs.git/'
                 }
             }
             stage('Build'){
                 steps {
                     sh 'sudo docker build . -t bandank/nodejs-app:latest'
                 }
             }
             stage('Run'){
                 steps {
                     sh 'sudo docker run -d -p 8000:8000 bandank/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.

Task-2 : Using 'docker' groovy syntax

  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.

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

Thanks for reading my article. Have a nice day.

You can follow me on LinkedIn for my daily updates:- linkedin.com/in/bandan-kumar-sahoo-131412203

More from this blog

Untitled Publication

98 posts