Skip to main content

Overview

Leveraging a Dockerfile agent in Jenkins lets you build a custom Docker image with all the CLI tools your pipeline requires. This approach ensures:
  • Consistent environments across builds
  • Isolation for each job
  • Flexibility to install any SDKs, CLIs, or dependencies

Prerequisites

Ensure your Jenkins agent node has Docker installed and that the Jenkins user has permission to run Docker commands.

Step 1: Example with the Standard Node.js Alpine Image

Here’s a simple Jenkinsfile using the official node:18-alpine image:
When executed, the first two commands succeed, but cowsay is missing:
The image shows a Jenkins dashboard displaying the activity of a pipeline named "pipeline-external-agent," with details on the status, run number, message, duration, and completion time of recent builds.

Step 2: Create a Custom Dockerfile

To include cowsay, create Dockerfile.cowsay:
This Dockerfile:
  1. Updates the Alpine package index
  2. Installs Git and Perl
  3. Clones and installs cowsay
  4. Cleans up temporary files
Always remove temporary directories like /tmp/cowsay to keep your image size small.

Step 3: Switch to the dockerfile Agent

Update your Jenkinsfile to build the custom image on the fly:
Behind the scenes, Jenkins will:
  1. Read Dockerfile.cowsay from the workspace
  2. Run:
  3. Launch a container from the new image and execute your steps

Step 4: Confirm Successful Build

In the Jenkins console you’ll see:
All commands now succeed, including cowsay.

Watch Video