Docker command not found in local Jenkins multi branch pipeline

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.


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>

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

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