Skip to main content
Learn how to build a custom Docker image via a Dockerfile and use it as an agent in a Jenkins Pipeline stage. This method lets you pre-install all required tools—like Node.js and cowsay—so your CI/CD steps run smoothly.

1. Using the Standard node:18-alpine Docker Agent

First, try running Node.js commands and the cowsay utility using the official node:18-alpine image:
Commit and push this Jenkinsfile, then trigger a build.
The image shows a Jenkins dashboard displaying the activity of a pipeline named "pipeline-external-agent," with details of recent runs, their status, duration, and completion times.
The S4-Dockerfile Agent stage will fail because cowsay is not installed in the base image.

2. Diagnosing the Missing Utility

Inspect the console output to confirm which utility is missing:
  • Node.js commands succeed (node -v, npm -v)
  • cowsay fails: the utility isn’t present in node:18-alpine.

3. Crafting a Custom Dockerfile

Create Dockerfile.cowsay at your repository root to bundle Node.js and cowsay:
This Dockerfile:
  1. Base Image: node:18-alpine
  2. Dependencies: Installs git and perl via Alpine’s package manager
  3. Cowsay: Clones the cowsay GitHub repository and runs its installer
Using Alpine keeps the image slim. Ensure you list all required packages in the RUN apk add command.

4. Updating Your Jenkinsfile to Use the Custom Image

Swap the docker agent block for a dockerfile block:
Commit and push. Jenkins will now build your custom image before executing the stage.

5. Observing the Build Output

When you trigger the build, you’ll see:
Once the image builds, Jenkins runs your container and executes the steps:
Now both Node.js and cowsay run successfully inside the container.

6. Agent Types Comparison

7. Conclusion

By leveraging a custom Dockerfile agent, you ensure each Jenkins stage has exactly the tools it needs. This strategy:
  • Simplifies dependency management
  • Keeps agents lightweight
  • Improves reproducibility

Watch Video