Configuring Docker Cloud in Jenkins with Groovy Scripts
This tutorial walks through using Groovy code to set up Docker Cloud in Jenkins, detailing class imports, parameter maps, template creation, cloud configuration, and an alternative JNLP‑based example for advanced container launch strategies.
After setting up Jenkins CI in Docker, this guide explains how to configure Docker Cloud on Jenkins using Groovy code.
It starts by importing required classes from the docker‑plugin, such as import com.nirima.jenkins.plugins.docker.DockerCloud, import com.nirima.jenkins.plugins.docker.DockerTemplate, import com.nirima.jenkins.plugins.docker.DockerTemplateBase,
import com.nirima.jenkins.plugins.docker.launcher.AttachedDockerComputerLauncher,
import io.jenkins.docker.connector.DockerComputerAttachConnector, and import jenkins.model.Jenkins.
Next, it defines the basic parameters for a Docker template in a map called dockerTemplateBaseParameters, specifying image, ports, memory limits, environment variables, and other container options.
Another map DockerTemplateParameters holds node‑specific settings like instance cap, label, and remote filesystem.
Using these maps, a
DockerTemplateBase dockerTemplateBase = new DockerTemplateBase( ... )object is instantiated, then a
DockerTemplate dockerTemplate = new DockerTemplate( dockerTemplateBase, new DockerComputerAttachConnector(), DockerTemplateParameters.labelString, DockerTemplateParameters.remoteFs, DockerTemplateParameters.instanceCapStr )is created.
The cloud configuration is described in dockerCloudParameters, including connection timeout, container cap, credentials, server URL, and Docker version.
Finally, a
DockerCloud dockerCloud = new DockerCloud( dockerCloudParameters.name, [dockerTemplate], dockerCloudParameters.serverUrl, dockerCloudParameters.containerCapStr, dockerCloudParameters.connectTimeout, dockerCloudParameters.readTimeout, dockerCloudParameters.credentialsId, dockerCloudParameters.version, dockerCloudParameters.dockerHostname )object is built, added to the Jenkins instance via
Jenkins jenkins = Jenkins.getInstance(); jenkins.clouds.add(dockerCloud); jenkins.save();.
The article also provides an alternative example for a JNLP‑type Docker cloud, showing additional imports (
import com.nirima.jenkins.plugins.docker.launcher.DockerComputerJNLPLauncher,
import io.jenkins.docker.connector.DockerComputerJNLPConnector, import hudson.slaves.JNLPLauncher) and a modified DockerTemplate constructor that includes user and Jenkins URL settings:
new DockerTemplate( dockerTemplateBase, new DockerComputerJNLPConnector(new JNLPLauncher(null, null)).withUser("jenkins").withJenkinsUrl("http://123.123.123:8080"), DockerTemplateParameters.labelString, DockerTemplateParameters.remoteFs, DockerTemplateParameters.instanceCapStr ).
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
DevOps Cloud Academy
Exploring industry DevOps practices and technical expertise.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
