Search code examples
dockerjenkinsjenkins-pipeline

Docker command not found in local Jenkins multi branch pipeline


I have BookStore Spring Boot project that needs to be deployed through Jenkins. Docker installed in my local machine (macOS) and Jenkinsfile created as follows

pipeline 
{
     agent 
     {
         docker
          {
            image 'maven:3-alpine'
            //This exposes application through port 8081 to outside world
            args '-u root -p 8081:8081 -v /var/run/docker.sock:/var/run/docker.sock  '
         }
    } 
    stages 
     {
        stage('Build') 
         {
              steps 
              {
                sh 'mvn -B -DskipTests clean package'
              }
          }

        stage('Test') 
        {
            steps {
                //sh 'mvn test'
                sh 'echo "test"'
            }
            post {
                always {
                    //junit 'target/surefire-reports/*.xml'
                    sh 'echo "test"'
                }
            }
        }

        stage('Deliver for development')
        {
                    when {
                        branch 'development'
                    }
                    steps {
                        sh './jenkins/scripts/deliver-for-development.sh'
                        input message: 'Finished using the web site? (Click "Proceed" to continue)'
                    }
        }

        stage('Deploy for production')
        {
            when {
                branch 'production'
            }
            steps {
                sh './jenkins/scripts/deploy-for-production.sh'
                input message: 'Finished using the web site? (Click "Proceed" to continue)'
            }
        }

        stage('Deliver') {
        when {
              branch 'production'
           }
            steps {
                sh 'bash ./jenkins/deliver.sh'
            }
        }
    }

}

I created multi-branch pipeline in Jenkins and when I try to run it, I got following error

/Users/Shared/Jenkins/Home/workspace/BookStore_master-VPWQ32ZZPV7CVOXNI4XOB3VSGH56MTF3W34KXKZFJKOBMSGLRZQQ@tmp/durable-70dd5a81/script.sh: line 2: docker: command not found

script returned exit code 127

This looks strange to me as docker available in local machine, and also configured Global Tool Configuration section with appropriate details as shown below. I looked into several posts and none of the solutions worked so far.

Global Tool Configuration


Solution

  • I was able to solve this by retrieving Docker and Maven values from Global Tool Configuration section and adding them to environment PATH as shown below

    Updated Jenkinsfile:

    node {
    
        stage('Initialize')
        {
            def dockerHome = tool 'MyDocker'
            def mavenHome  = tool 'MyMaven'
            env.PATH = "${dockerHome}/bin:${mavenHome}/bin:${env.PATH}"
        }
    
        stage('Checkout') 
        {
            checkout scm
        }
    
          stage('Build') 
               {
                sh 'uname -a'
                sh 'mvn -B -DskipTests clean package'  
              }
    
            stage('Test') 
            {
                //sh 'mvn test'
                sh 'ifconfig' 
            }
    
            stage('Deliver') 
              {
                    sh 'bash ./jenkins/deliver.sh'
            }
    }