Pavan Jadda
Pavan Jadda

Reputation: 4871

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

Upvotes: 15

Views: 55675

Answers (11)

hongkail
hongkail

Reputation: 789

MacOS with jenkins-lts version 2.452.1,

If you are using Ocean Blue, i.e. access with browser at http://localhost:8080/

  1. You may go to http://localhost:8080/computer/
  2. Look for the node you are using for the job (the node you must have labelled with "workers" (if your Jenkinsfile stated node('workers').
  3. Click the settings icon located at the most right of the node item, and under "Node Properties", select "Environment variable", copy and paste the entire environment PATH that contains the docker command, eg. you may copy and paste the entire PATH if you are using linux/macos from echo $PATH or specific docker path from which $PATH

You may refer to this link to see what else to add in your PATH https://www.jenkins.io/doc/book/pipeline/docker/#path-setup-for-mac-os-users

Upvotes: 0

mkarthik415
mkarthik415

Reputation: 125

For Mac user, adding path to Docker in tools should do the trick. please check the screenshots.

Adding path to Docker in tools should do the trick.

Also update Jenkinsfile

enter image description here

Upvotes: 0

Nickofthyme
Nickofthyme

Reputation: 4327

I had the same issue and was able to resolve it thanks to this thread https://stackoverflow.com/a/50029962/6943587.

You need to specify the docker label, aka which agent(s) have docker. There are two ways to do this, that I know of.

(Option 1 - preferred) Set docker label in Jenkinsfile

Set the agent as docker image with docker agent label.

// Jenkinsfile

pipeline {
  // Assign to docker agent(s) label, could also be 'any'
  agent {
    label 'docker' 
  }

  stages {
    stage('Docker node test') {
      agent {
        docker {
          // Set both label and image
          label 'docker'
          image 'node:7-alpine'
          args '--name docker-node' // list any args
        }
      }
      steps {
        // Steps run in node:7-alpine docker container on docker agent
        sh 'node --version'
      }
    }

    stage('Docker maven test') {
      agent {
        docker {
          // Set both label and image
          label 'docker'
          image 'maven:3-alpine'
        }
      }
      steps {
        // Steps run in maven:3-alpine docker container on docker agent
        sh 'mvn --version'
      }
    }
  }
} 

(Option 2) Set docker label in configuration

Set the "docker label" in the Jenkins configuration under "Pipeline Model Definition", per the Jenkins docs here. This will only run the pipeline builds on agents with this label. Then you can create your pipeline like so...

// Jenkinsfile

pipeline {
  // "Top-level" agent is assigned to docker agents via Jenkins pipeline configuration
  agent none

  stages {
    stage('Docker node test') {
      agent {
        docker {
          image 'node:7-alpine'
          args '--name docker-node' // list any args
        }
      }
      steps {
        // Steps run in node:7-alpine docker container on docker agent
        sh 'node --version'
      }
    }
    
    stage('Docker maven test') {
      agent {
        docker {
          image 'maven:3-alpine'
        }
      }
      steps {
        // Steps run in maven:3-alpine docker container on docker agent
        sh 'mvn --version'
      }
    }
  }
}

Hope this helps

Option 1 is preferred over option 2 because the Jenkinsfile configures what machine(s) to run the docker agents on without relying on the Jenkins pipeline configuration which could be deleted or edited in the future.

Upvotes: 3

Kalyan Raparthi
Kalyan Raparthi

Reputation: 435

add -v $(which docker):/usr/bin/docker while running container

Upvotes: -1

Khalid Hafid-Medheb
Khalid Hafid-Medheb

Reputation: 11

Just apply the line separator Unix and Mac Os : "\n" in your ".sh" files with your code editor. It worked for me.

Upvotes: 0

prashantsahni
prashantsahni

Reputation: 2195

If you are on windows Follow from here:- https://www.katacoda.com/courses/jenkins/build-docker-images

Upvotes: 0

dhanushka
dhanushka

Reputation: 392

I faced the same issue on the Mac and the following answer helped me.

docker: command not found ( mac mini ) only happens in jenkins shell step but work from command prompt.

The solution is to add the following line into the /usr/local/Cellar/jenkins-lts/2.176.3/homebrew.mxcl.jenkins-lts.plist file so that Jenkins able to find the docker command from the host machine.

<key>EnvironmentVariables</key>
    <dict>
      <key>PATH</key>
      <string>/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Applications/Docker.app/Contents/Resources/bin/:/Users/Kh0a/Library/Group\ Containers/group.com.docker/Applications/Docker.app/Contents/Resources/bin</string>
    </dict>

Upvotes: 7

wind_surfer
wind_surfer

Reputation: 39

In my case I had docker command issues because I was using jenkins-lts which is also a docker. After trying to debug for quite a while, I realized referencing docker command with in a docker might be an issue. I stopped the jenkins-lts service, downloaded jenkins.war file and ran the same pipeline script with docker command. It started working. My pipeline script has agent any, it still works in jenkins.war version of jenkins

Upvotes: 0

PankajSays
PankajSays

Reputation: 1045

There seems to be an issue with automated docker installer. I encountered the same issue on docker on centos 7.

I downloaded the docker cli executables from https://download.docker.com/linux/static/stable/x86_64/ and extracted them into jenkins docker volume on host (/var/lib/docker/volumes/jenkins_home/_data/docker). Then copied from /var/jenkins_home/docker to /usr/bin using shell on docker container.

After coping the executables, the build worked as expected.

Upvotes: 0

Pavan Jadda
Pavan Jadda

Reputation: 4871

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'
        }
}

Upvotes: 0

Ayush Verma
Ayush Verma

Reputation: 68

Since you have chosen install automatically option in Global Tool Configuration section, Jenkins will not look for the docker in your system.

You can resolve this issue by unchecking the install automatically option for docker in Global Tool Configuration section

  • download docker installer,
  • install it and
  • give the path of installer to Jenkins.

Example screenshot is below.

Setup docker installer path in jenkins under Global Tool Configuration

Upvotes: 1

Related Questions