Chapter 14. Integrating Containers

Docker forms a key component in many pipelines these days. The ease, flexibility, and isolation provided by containers allows us to create custom, specific environments for processing with exact repeatability. In this chapter, we’ll look at the different ways that Docker can be used with Jenkins 2.

Prerequisite Knowledge

This chapter assumes that you are familiar with the basic concepts and use of Docker separate from Jenkins. If that is not the case, it will be helpful to consult some of the online training materials and documentation widely available for Docker before continuing.

For Jenkins 2, there are essentially four options for incorporating Docker into your pipeline:

  • Configured as a “cloud,” as a standalone Jenkins agent

  • As an agent created on the fly for a Declarative Pipeline

  • Via the special DSL docker global variable and its associated methods

  • Directly in the script via the DSL shell call (sh)

Let’s take a closer look at each of these.

Configured as a Cloud

The idea here is that you are defining one or more Docker images that Jenkins can use as agents. This is the “cloud” environment from which to start up agents. When your pipeline runs, it can reference the cloud setup and start up instances of the images as agents. The agents can then be used to run the various stages and steps. After the pipeline is done, Jenkins will stop and remove the containers running those images, thus removing the agents.

In order for this option to ...

Get Jenkins 2: Up and Running now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.