Pipeline - I would like to use Docker CLI inside of a Pipeline job

Article ID:230922468
1 minute readKnowledge base

Issue

I would like to use Docker CLI inside of a Pipeline job with the Pipeline Docker Plugin

this article does not apply to a Pipeline running in a containerized agent.

Resolution

Wrapping the section you want to use docker in with docker.withTool("default") {} will allow you to use the docker command line tool. This will install docker on that agent to have it running as a service. You need to make sure that "default" points to a docker tool inside of Manage Jenkins> Configure System which will also run on the os of the agent.

Your pipeline script will go from this(which doesnt work unless you have docker installed on the agent):

withDockerServer([credentialsId: "AWS-Jenkins-Build-Slave", uri: "tcp://54.229.21.138:2376"]) {

sh "printenv"
sh "docker images"
base = docker.build("flyvictor/victor-wp-build")
base.push("tmp-fromjenkins")
}

To this(which does work):

docker.withTool("default") {

withDockerServer([credentialsId: "AWS-Jenkins-Build-Slave", uri: "tcp://54.229.21.138:2376"]) {

sh "printenv"
sh "docker images"
base = docker.build("flyvictor/victor-wp-build")
base.push("tmp-fromjenkins")
}
}

References