Docker command in Jenkinsfile cannot be executed when Jenkins is built.

  docker, question

When using docker and Jenkins to build the project, the execution of the Docker statement in Jenkinsfile reported the following error:

[Pipeline] }
 [Pipeline] // dir
 [Pipeline] }
 [Pipeline] // stage
 [Pipeline] stage
 [Pipeline] { (Create Docker Image)
 [Pipeline] dir
 Running in /Users/larry/.jenkins/workspace/docker-jenkins-pipeline/RpcServerSample
 [Pipeline] {
 [Pipeline] }
 [Pipeline] // dir
 [Pipeline] }
 [Pipeline] // stage
 [Pipeline] }
 [Pipeline] // node
 [Pipeline] End of Pipeline
 groovy.lang.MissingPropertyException: No such property: docker for class: groovy.lang.Binding
 at groovy.lang.Binding.getVariable(Binding.java:63)
 at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onGetProperty(SandboxInterceptor.java:224)
 at org.kohsuke.groovy.sandbox.impl.Checker$4.call(Checker.java:241)
 at org.kohsuke.groovy.sandbox.impl.Checker.checkedGetProperty(Checker.java:238)
 at org.kohsuke.groovy.sandbox.impl.Checker.checkedGetProperty(Checker.java:221)
 at org.kohsuke.groovy.sandbox.impl.Checker.checkedGetProperty(Checker.java:221)
 at org.kohsuke.groovy.sandbox.impl.Checker.checkedGetProperty(Checker.java:221)
 at com.cloudbees.groovy.cps.sandbox.SandboxInvoker.getProperty(SandboxInvoker.java:24)
 at com.cloudbees.groovy.cps.impl.PropertyAccessBlock.rawGet(PropertyAccessBlock.java:20)
 at WorkflowScript.run(WorkflowScript:12)
 at ___cps.transform___(Native Method)
 at com.cloudbees.groovy.cps.impl.PropertyishBlock$ContinuationImpl.get(PropertyishBlock.java:74)
 at com.cloudbees.groovy.cps.LValueBlock$GetAdapter.receive(LValueBlock.java:30)
 at com.cloudbees.groovy.cps.impl.PropertyishBlock$ContinuationImpl.fixName(PropertyishBlock.java:66)
 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
 at java.lang.reflect.Method.invoke(Method.java:497)
 at com.cloudbees.groovy.cps.impl.ContinuationPtr$ContinuationImpl.receive(ContinuationPtr.java:72)
 at com.cloudbees.groovy.cps.impl.ConstantBlock.eval(ConstantBlock.java:21)
 at com.cloudbees.groovy.cps.Next.step(Next.java:58)
 at com.cloudbees.groovy.cps.Continuable.run0(Continuable.java:154)
 at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.access$001(SandboxContinuable.java:18)
 at org.jenkinsci.plugins.workflow.cps.SandboxContinuable$1.call(SandboxContinuable.java:33)
 at org.jenkinsci.plugins.workflow.cps.SandboxContinuable$1.call(SandboxContinuable.java:30)
 at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.GroovySandbox.runInSandbox(GroovySandbox.java:108)
 at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.run0(SandboxContinuable.java:30)
 at org.jenkinsci.plugins.workflow.cps.CpsThread.runNextChunk(CpsThread.java:163)
 at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.run(CpsThreadGroup.java:324)
 at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.access$100(CpsThreadGroup.java:78)
 at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:236)
 at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:224)
 at org.jenkinsci.plugins.workflow.cps.CpsVmExecutorService$2.call(CpsVmExecutorService.java:63)
 at java.util.concurrent.FutureTask.run(FutureTask.java:266)
 at hudson.remoting.SingleLaneExecutorService$1.run(SingleLaneExecutorService.java:112)
 at jenkins.util.ContextResettingExecutorService$1.run(ContextResettingExecutorService.java:28)
 at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
 at java.util.concurrent.FutureTask.run(FutureTask.java:266)
 at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
 at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
 at java.lang.Thread.run(Thread.java:745)

Jenkinsfile reads as follows:

node {
 checkout scm
 //env.PATH = "${tool 'Maven3'}/bin:${env.PATH}"
 stage('Package') {
 dir('RpcServerSample') {
 sh 'mvn clean package -DskipTests'
 }
 }
 
 stage('Create Docker Image') {
 dir('RpcServerSample') {
 docker.build("daocloud.io/suqun/docker-jenkins-pipeline:${env.BUILD_NUMBER}")
 }
 }
 
 stage ('Run Application') {
 try {
 // Start database container here
 // sh 'docker run -d --name db -p 8091-8093:8091-8093 -p 11210:11210 arungupta/oreilly-couchbase:latest'
 
 // Run application using Docker image
 //            sh "DB=`docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' db`"
 sh "docker run -d -p 9001:9001 daocloud.io/suqun/docker-jenkins-pipeline:${env.BUILD_NUMBER}"
 
 // Run tests using Maven
 //dir ('webapp') {
 //  sh 'mvn exec:java -DskipTests'
 //}
 } catch (error) {
 } finally {
 // Stop and remove database container here
 //sh 'docker-compose stop db'
 //sh 'docker-compose rm db'
 }
 }
 
 stage('Run Tests') {
 try {
 dir('RpcServerSample') {
 sh "mvn test"
 docker.build("daocloud.io/suqun/docker-jenkins-pipeline:${env.BUILD_NUMBER}").push()
 }
 } catch (error) {
 
 } finally {
 junit '**/target/surefire-reports/*.xml'
 }
 }
 }

The statement reporting the error is: doc.build (“Dao cloud.io/suqun/doc-Jenkins-pipeline: $ {env.build _ number}”)

Guess if groovy’s plug-in isn’t installed?

The problem has been resolved and Jenkins’ plug-in is missing.
Search for CloudBees Docker Pipeline in system management-> management plug-in-> optional plug-in and install it directly