An agent in our implementation of Jenkins is a pre-defined set of Docker images that can be used to perform a build step. The following snippet demonstrates declaring an agent and using the agent in a build step. Note that this is not a full pipeline, just the code that relates to an agent. 

...
  agent { 
    node { 
      label 'docker-builder'
    } // end node
  } // end agent
...
    // run a Docker build
    stage ('Docker build') {
      steps {
        container('docker') {
          echo 'docker build'
          script {
            docker.withRegistry('https://REGISTRY.artifactory.sdodev.leidos.com/', 'CREDENTIAL'){
              docker.build('IMAGE:TAG', '.')
            } // end docker
          } // end script
        } //end container
      } // end steps
    } //end stage

Notes:

Line 4: The agent is used to build Docker images. Please see the list of other agents below that have been configured.

Line 11:  An agent consists of one or more additional Docker containers. Some commands will only work when issued from within the selected container. This step is selecting the container named 'docker' which contains the the Docker CLI.

Line 14: This line selects the registry from which layers will be pulled when building images. Since we are using private registries, a set of credentials are needed for docker to log into the registry.

Line 15: This will build an image (the first parameter) setting the current directory as the Docker context (the second parameter).

Important: This stage will build the image but not upload the image to a registry. The code snippet below will upload the built image to a registry:

    // push image
    stage ('Docker push latest') {
      steps {
        container('docker') {
          
          script{
            docker.withRegistry('https://REGISTRY.artifactory.sdodev.leidos.com/', 'CREDENTIAL'){
              docker.push('IMAGE:TAG')
            } //end docker
		  } //end script
        } // end container
      } // end steps
    } //end stage

An alternate way to perform Docker builds is as follows:

pipeline{
  agent{
    node('docker-builder')
  } //end agent

  environment {
    REGISTRY = "sdo-docker.artifactory.sdo.leidos.com"
    IMAGE = "docker-test"
    TAG = "1.0"
  } // end environment

  stages{
    stage ('Docker build') {
      steps{
        container('docker') {
          withCredentials([usernamePassword(credentialsId:'docker-test', usernameVariable: 'USER', passwordVariable: 'PASSWORD')]) {
            sh '''
              docker login -u ${USER} -p ${PASSWORD} ${REGISTRY}
              docker build -f docker/Dockerfile -t ${REGISTRY}/${IMAGE}:${TAG} .
              docker push ${REGISTRY}/${IMAGE}:${TAG}
            ''' // end sh
          } //end withCredentials
        } // end container
      } // end steps
    } // end stage

  } // end stages
} // end pipeline

Line 16: the credentialsId contains username/password credentials stored in Jenkins that can login/push/pull in the named REGISTRY.

Additional Agents available in Jenkins

The following additional Agents are configured in Jenkins:

Namepurpose
defaultThe default agent, provides a JNLP connection back to Jenkins, contains maven and a JDK (1.8)
openjdk11Contains OpenJDK 11.0.5, container name is openjdk11
docker-builderContains an image ('docker') with the Docker CLI for building Docker images (using podman)
taurusContains a Taurus image ('taurus') for running various test automations. A default JMeter automation framework is available in this image
xl-builder

Similar to default but allocates more CPU and memory resources. Some builds may require more resources than the 'default' agent.

jenkins-tools-python3

container name is python3

jenkins-tools-js

container name is js

cmake3

container name is cmake3, contains gcc, g++, make, cmake, cmake3

rpmbuild

container name is rpmbuild, contains rpmdevtools, rpmlint