Docker Agent

(this post if part of the material I cover in my devops course)

What is a docker agent?
How can we create one and use it? We'll explore this on the next post(2)

What is a docker agent

Let's say that I want to run some nodejs javascript code.
I don't want to really install nodejs on any of my agents.
Can I use docker?
Yes!!! I can specify a docker agent in my pipeline.
Jenkins will run a container from the specified docker images, and run my stage(s) in it:

 1pipeline {
 2    agent {
 3        docker { image 'node:20.10.0-alpine3.19' }
 4    }
 5    stages {
 6        stage('Test') {
 7            steps {
 8                sh 'node --version'
 9            }
10        }
11    }
12}

Stage level docker agent

Agent declaration could be used locally to a stage.
In this case I have declared agent none so that I define stage level agents.
(read nore about agents here)

 1pipeline {
 2    agent none
 3    stages {
 4        stage('Test') {
 5            agent {
 6                docker { image 'node:20.10.0-alpine3.19' }
 7            }
 8            steps {
 9                sh 'node --version'
10            }
11        }
12        stage('hello'){
13            agent any
14            steps {
15                echo 'hello'
16            }
17        }
18    }
19}